当我在repl.it中编译C程序时,它运行得很好,但do/while循环在代码块中不起作用



我的C入门编程类让我们在repl.it上使用编译器。但我雄心勃勃,想在爬行之前尝试运行,所以我下载了codeBlocks来了解更多关于IDE的信息。

虽然我自己设置编辑器和编译器的感觉很好,但我现在遇到了一个问题,也许你们中的一个人可以回答。首先,我要发给你们的代码是家庭作业的一部分,但它已经完成了,这不是我如何做X来得到Y。这是一个关于为什么我的do/while循环在repl.it中工作,而不是代码块的问题!

其次,这是我的代码:

int main( void )
{
int classSel; // variable for what class consumer chooses
int seat[SIZE] = {0}; // initializing all seats to zero or false meaning 
//they are empty
char nextFlight;
unsigned int i, j = 1, k = 6;
do
{
printf( "nChoose only available options. Enter 1 (First Class; Seats 
Left: %d) or 2 (Economy Class Seats Left: %d) ", 6 - j, 11 - k );
scanf( "%d", &classSel );
if ( classSel == 1 )
{
if ( seat[j] == 0 && j < 6 )
{
seat[j] = 1;
printf( "n******************n" );
printf( "*  COP AIRLINES  *n" );
printf( "*  FIRST  CLASS  *n" );
printf( "*   Seat No. %d   *n", j );
printf( "******************n" );
j++;
i++;
printf( "nnPage will reload for next customer...n" );
}
else
{
printf( "First Class seats are full. Do you want Economy Class instead? (Press Y or N)");
scanf( " %c", &nextFlight );
if ( nextFlight == 'Y' || nextFlight == 'y')
{
if ( seat[k] == 0 && k < SIZE )
{
printf( "nYou chose to stay on this flight! You must be ready to get where you want to go!n");
seat[k] = 1;
printf( "n******************n" );
printf( "*  COP AIRLINES  *n" );
printf( "*   ECON CLASS   *n" );
printf( "*   Seat No. %d   *n", k );
printf( "******************n" );
printf( "nnPage will reload for next customer...n" );
k++;
i++;
}
}
else if ( nextFlight == 'N' || nextFlight == 'n' )
{
printf( "nThe next flight is in three hours. See you then!nnPage will reload for next customer...n");
}      }
}
else if ( classSel == 2 )
{
if ( seat[k] == 0 && k < SIZE )
{
seat[k] = 1;
printf( "n******************n" );
printf( "*  COP AIRLINES  *n" );
printf( "*   ECON CLASS   *n" );
printf( "*   Seat No. %d   *n", k );
printf( "******************n" );
printf( "nnPage will reload for next customer...n" );
k++;
i++;
}
else
{
printf( "Economy class seats are full. Do you want First Class instead? (Press Y or N)");
scanf( " %c", &nextFlight );
if ( nextFlight == 'Y' || nextFlight == 'y')
{
if ( seat[j] == 0 && j < 6 )
{
printf( "nYou chose to upgrade on this flight. Great choice!n" );
seat[j] = 1;
printf( "******************n" );
printf( "*  COP AIRLINES  *n" );
printf( "*  FIRST  CLASS  *n" );
printf( "*   Seat No. %d   *n", j );
printf( "******************n" );
printf( "nnPage will reload for next customer...n" );
j++;
i++;
}
}
else if ( nextFlight == 'N' || nextFlight == 'n' )
{
printf( "nThe next flight is in three hours. See you then!nnPage will reload for next customer...n");
}
}
}
} while( i < 10 );
printf( "Plane is full. Sorry but you must wait for the next flight. It is 
in 3 hours.n");
}

出于某种原因,当我展示我对尺寸(11)的定义时,或者当我包含<stdio.h>时,它不喜欢,但我的代码中都有这两个!

第三,我不想让你评论我的代码有多丑陋和粗糙,我很清楚这一点。我只想知道为什么do/while循环在repl.it中工作得很完美,但它只经过codeBlocks中的一个循环。

最可能的原因是您没有初始化i

i = 0;放在代码的顶部(并初始化其他变量)

有些环境先清空内存(使0为0),有些没有,有些甚至初始化为非零,以更容易发现未初始化的变量问题

此外,希望你的编译器应该警告你这些事情,通常在新的时候你不知道如何修复警告,而且通常代码无论如何都能工作,但要学会消除所有警告,它们往往会帮助你解决这样的问题。

相关内容

最新更新