当我在repl.it中编译它时,我的C程序运行良好但是do / while循环在代码块

时间:2017-10-26 02:10:39

标签: c loops do-while

我的编程类简介让我们在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( "\n\nPage 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( "\n\nPage 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!\n\nPage 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( "\n\nPage 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( "\n\nPage 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!\n\nPage 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");
}

出于某种原因,当我展示我对SIZE(其中的11)的定义或者当我包括<stdio.h>时,它并不喜欢,但我在我的代码中已经有了这样的明白你的想法!

第三,我不是在找你评论我的代码是多么丑陋和笨拙,我非常清楚这一点。我想知道的是为什么do / while循环在repl.it中完美无缺,但它只通过codeBlocks中的一个循环。

1 个答案:

答案 0 :(得分:2)

最可能的原因是您没有初始化data: Array(8) 0: {} 1: {} 2: {} 3: {} 4: {} 5: {} 6: {} 7: {}

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

某些环境空白记忆首先(制作0),有些甚至没有,有些甚至初始化为非零,以便更容易找到未初始化的变量问题

另外,希望您的编译器应该警告您这些事情,通常在新的时候您不知道如何修复警告,并且通常代码仍然有用,但学会杀死所有警告,它们往往会帮助您解决问题像这样。