2017-08-06 118 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);