user14330884
user14330884

Reputation:

Need to loop switch statement until 1 or 2 is entered

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

Upvotes: 2

Views: 159

Answers (2)

Lasan Nishshanka
Lasan Nishshanka

Reputation: 87

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

Upvotes: 2

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.

Upvotes: 3

Related Questions