2017-08-06 120 views
-2

我正在做一个学校项目,要求我在每个选项后返回到菜单,但我目前遇到使用do-while循环返回菜单的麻烦,因为我的while(true)由于(真)未定义而导致运行问题。请帮忙!我的while循环不起作用

do { 
    // print out menu 
    printf("================================== MENU ==================================== \n"); 
    printf("HELLO PLEASE CHOOSE 1 OPTION BELOW:           \n"); 
    printf("(1) CO2 reading and health advisory descriptor of a given classroom and time \n"); 
    printf("(2) 3 hourly average CO2 reading for a selected classroom     \n"); 
    printf("(3) Highest CO2 reading from the classroom for a selected time    \n"); 
    printf("(4) Top 3 unhealthy readings for a selected classroom      \n"); 
    printf("(5) List of time periods and classroom with above 'Average' value   \n"); 
    printf("(6) The unhealthiest classroom CO2 reading from 7am to 11am     \n"); 
    printf("============================================================================ \n"); 
    printf("\n"); 

    // getting user input 
    printf("Please enter your option: "); 
    int userInput; 
    scanf_s("%d", &userInput); // put the user input into int userInput 
    printf("\n"); 

    // check for the user input and run the function accordingly 
    switch (userInput) 
    { 
    case 1: // if the user press 1 
    { 
     // call option1 function 
     option1(); 
     break; 
    } 
    case 2: 
    { 
     // call option2 function 
     option2(); 
     break; 
    } 
    case 3: 
    { 
     // call option3 function 
     option3(); 
     break; 
    } 
    case 4: 
    { 
     // call option4 function 
     option4(); 
     break; 
    } 
    case 5: 
    { 
     // call option5 function 
     option5(); 
     break; 
    } 
    case 6: 
    { 
     // call option6 function 
     option6(); 
     break; 
    } 
    } 
} while (true); 

这是为什么?

+5

尝试'while(1)'。 – ForceBru

+3

'while while(1)'或'#include '。 – randomir

+0

谢谢while(1)works! :) – Danzel

回答

3

只需包含<stdbool.h>即可使用true和false布尔变量。

检查this了解更多详情。

0

我认为它的更好,如果你:

  • 添加一个“退出”选项,在你的菜单和而不是使用一个无限循环
  • 声明环

所以这是外面的变量你的代码是什么样的:

int userInput=0; 
    int endLoopCondition=1;//Meaning true 
    do { 
    // print out menu 
    printf("================================== MENU ==================================== \n"); 
    printf("HELLO PLEASE CHOOSE 1 OPTION BELOW:           \n"); 
    printf("(1) CO2 reading and health advisory descriptor of a given classroom and time \n"); 
    printf("(2) 3 hourly average CO2 reading for a selected classroom     \n"); 
    printf("(3) Highest CO2 reading from the classroom for a selected time    \n"); 
    printf("(4) Top 3 unhealthy readings for a selected classroom      \n"); 
    printf("(5) List of time periods and classroom with above 'Average' value   \n"); 
    printf("(6) The unhealthiest classroom CO2 reading from 7am to 11am     \n"); 
    printf("(7) QUIT                  \n"); 
    printf("============================================================================ \n"); 
    printf("\n"); 

    // getting user input 
    printf("Please enter your option: "); 

    scanf_s("%d", &userInput); // put the user input into int userInput 
    printf("\n"); 

    // check for the user input and run the function accordingly 
    switch (userInput) 
    { 
     case 1: // if the user press 1 
     { 
      // call option1 function 
      option1(); 
      break; 
     } 
     case 2: 
     { 
      // call option2 function 
      option2(); 
      break; 
     } 
     case 3: 
     { 
      // call option3 function 
      option3(); 
      break; 
     } 
     case 4: 
     { 
      // call option4 function 
      option4(); 
      break; 
     } 
     case 5: 
     { 
      // call option5 function 
      option5(); 
      break; 
     } 
     case 6: 
     { 
      // call option6 function 
      option6(); 
      break; 
     } 
     case 7: 
     { 
      endLoopCondition=0; //when the user chose 7 the condition is false so the loop will stop 
      break; 
     } 
    } 
    } while (endLoopCondition);