c - 我的 while 循环不工作

标签 c while-loop do-while

我正在做一个学校项目,要求我在每个选项后返回到菜单,但我目前在使用 do-while 循环返回菜单时遇到问题,因为我的 while(true) 因运行而出现问题(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("============================================================================ \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);

为什么会这样呢?

最佳答案

只需包含 <stdbool.h>使用 true 和 false bool 变量。

检查this了解更多详情。

关于c - 我的 while 循环不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45534396/

相关文章:

char * vs unsigned char* 有什么区别?

while-loop - 为什么不在 Fortran 中使用 DO WHILE

matlab - Fortran 90 中 do while 和 do while 之间的区别

java - 向现有的 java 程序添加循环?

c++ - Objective-C 中的 AOP : Inject context-aware code into each method while maintaining DRY

c - C 中 system() 的问题

将带十六进制的无符号整数转换为字符串

r - 如何避免在 while 循环之外编码第一次迭代

php - 使用不同数量的文本框更新 mysql 数据库

c - While循环导致程序崩溃