2

I need to loop the switch statement until 1 or 2 is entered as an input. Any help would be nice or sample code. I tried to use a while loop and it kept crashing. I tried a do while as well and it turned into an infinite loop so I must be doing something wrong.

int main (void)
{
    int choice; // choice to run program from start menu or quit
    
    while (1)
    {
        // Menu for entering resistor and capacitor values
        printf("Welcome to the 555 Timer Frequency and Duty Cycle Calculator\n");
        printf("Please enter two resistor values in between ");
        printf("1 kOhms and 100 kOhms and a capacitor value\n\n");
 
        //Start of menu 
        printf("Menu\n\n");
        printf("1. Continue\n");
        printf("2. Exit\n");

        scanf("%d", &choice); // User inputs value


        switch (choice)
        {
        case 1:
            resistor(); // Program resistor is run if 1 is input
            break;
        case 2:
            printf("Goodbye."); // Program ends if 2 is input
            break; // break is used to exit while loop
        default:
            printf("Sorry I do not understand\n\n"); 
            fflush(stdin);  /* clears input buffer to allow user to input a new value if anything other
                               than 1 or 2 is input into scanf  */
        }

        break;
    }

    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • `fflush(stdin); ` why ? you need to flush the output buffer only – puio Sep 26 '20 at 18:56
  • how do I do that? –  Sep 26 '20 at 18:57
  • By passing `stdout` instead of stdin. But I don't see a crash https://godbolt.org/z/cnfv5W – puio Sep 26 '20 at 19:03
  • I need to flush the choice the user inputs which i thought would be stdin? Also, it is turning into an infinite loop whenever an integer or character other than 1 or 2 is entered,,,, I need it to print the statement "Sorry I do not understand" and then re run the menu –  Sep 26 '20 at 19:08
  • `fflush(stdin)` is undefined behaviour and should be avoided. – Marco Sep 26 '20 at 19:10
  • `break is used to exit while loop` -- This comment in your code is incorrect. In that place, the `break` statements exits the `switch` statement, not the outer `while` loop. If you want to exit the `while` loop from inside the `switch` statement, you will have to use a `goto` statement instead. – Andreas Wenzel Sep 26 '20 at 19:13
  • Be careful. A single `break` may terminate a loop or terminate a `switch`, depending on where it is placed - but not both of them! – CiaPan Sep 26 '20 at 19:18
  • Related: https://stackoverflow.com/questions/2187474/i-am-not-able-to-flush-stdin – user3386109 Sep 26 '20 at 19:31
  • It is unsafe to use the output of a `scanf` call without first checking the return value of `scanf`. See the following link for further information: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel Sep 26 '20 at 20:39

2 Answers2

3

The problem is that you have a "break" right at the end of your "while", so it's only executed once.

It has been a long time since I did program in C, but if I remember correctly you should avoid using "fflush()" since it has an undefined behavior. Instead, you can change your "scanf()" to be something like:

scanf(" %d", &choice); // User inputs value

That way, you have the same supposed effect of the "fflush()" at the same time of having a cleaner code.

2

You can use this code

for(;;) {
    // Menu for entering resistor and capacitor values

    // Start of menu

    // User inputs value

    if(choice == 1) { ... break; }
    else if(choice == 2) { ... return 0; } // If you use the break,the for loop will be stopped or You can use return 0; to return the whole function
    else { ... break; }
}

or, also you can use this code

for(;;) {
    switch(choice ) {
        case 1: ... break;
        case 2: ... return 0; // If you use the break, only switch statement is stopped. You can use return 0; to return the whole function
 
        default: ... break;
    }
}

Edit :

#include <stdio.h>

int main (void)
 {
int choice; // choice to run program from start menu or quit

// Menu for entering resistor and capacitor values
    printf("Welcome to the 555 Timer Frequency and Duty Cycle Calculator\n");
    printf("Please enter two resistor values in between ");
    printf("1 kOhms and 100 kOhms and a capacitor value\n\n");

while (1)
{
    
    //Start of menu 
    printf("Menu\n");
    printf("1. Continue\n");
    printf("2. Exit\n");
    
    scanf("%d", &choice); // User inputs value
    
    
    switch (choice)
        {
            case 1: // Program resistor is run if 1 is input
                break;
            case 2: printf("Goodbye."); // Program ends if 2 is input
                 return 0; // return 0; is used to exit while loop
                
            default: printf("Sorry I do not understand\nStart Again !\n"); 
                // fflush(stdin);  /* clears input buffer to allow user to input a new value if anything other than 1 or 2 is input into scanf     */
                
                
                break;
                
        }
        
        printf("\n");

    
}

return 0;
}