我的while循环不起作用

时间:2017-08-06 16:56:42

标签: 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);

为什么会这样?

2 个答案:

答案 0 :(得分:3)

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

查看this了解详情。

答案 1 :(得分:0)

我觉得如果你更好:

  • 添加&#34;退出&#34;菜单中的选项,而不是使用无限循环
  • 在循环之外声明您的变量

这样你的代码会是什么样子:

SELECT item.prop1, item.prop2,
       COALESCE(item.prop3, parents.prop3, grandparents.prop3) AS p3
FROM items
  INNER JOIN parents ON item.parent_id = parents.id
  INNER JOIN grandparents ON parents.grandparent_id = grandparents.id
  INNER JOIN pivot ON pivot.item_id = items.id
  INNER JOIN users ON pivot.user_id = users.id
WHERE
  items.prop4 IS NULL
  AND COALESCE(parents.prop5, grandparents.prop5) = 8
  AND users.country_id IN (123)