-2

I am doing a school project which requires me to return to the menu after each option but I am currently having trouble with returning to the menu using a do-while loop as my while(true) has an issue with running due to the (true) being undefined. Please help!

    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);

Why is this so?

Borgleader
  • 15,826
  • 5
  • 46
  • 62
Danzel
  • 9

2 Answers2

3

Just include <stdbool.h> to use true and false boolean variables.

Check this for more details.

0

I think its better if you:

  • add a "Quit" option in your menu and instead of using an infinite loop
  • declare your variables outside the loop

so that's what your code would look like:

    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);