2

This section of code is supposed to display a menu with two options, read the input and go to the corresponding function. If the user enters something other than 1 and 2 the program should warn the user and show the menu to ask user to enter the input again. This process will keep repeating until the user puts the right input.

I'm trying to find a way to repeat the loop only when the user inputs something other than 1 and 2 (so that the user can enter the appropriate response this time).

However, when I use a while loop like this, it loops no matter what the input is.

Any help is much appreciated.

char input;
displayWellDoneMenu();
scanf("%c", &input);

while (input != '1' || input != '2')
{
    printf("You must select 1 or 2!\n");
    displayWellDoneMenu();
    scanf("%c", &input);
    rewind(stdin);
    system("cls");
}

switch (input)
{
    case'1':
        additionIntermediate();
        break;
    case '2':
        main();
        break;
}
5
  • 6
    It loops no matter what the input is because input != '1' || input != '2' is always true no matter what input is. You want input != '1' && input != '2' Commented Jun 27, 2017 at 14:45
  • @lurker I see the problem now, thanks!! Commented Jun 27, 2017 at 14:50
  • You will also have a problem in that input isn't initialized the first time you test it. So I'd recommend setting it to some initial value not 1 or 2, or better, use one of the answers given by others which work around that issue. Commented Jun 27, 2017 at 14:57
  • When the user presses the Enter key, how should code handle that? by displaying "You must select 1 or 2!\n"? BTW: rewind(stdin); is not well defined. Commented Jun 27, 2017 at 16:05
  • Using rewind(stdin) is problematic. It may not do anything if the input is a terminal or pipe; it does nothing good and much harm if the input is coming from a file. An alternative that people often think of is using fflush(stdin), but that is also problematic. You should probablyuse " %c" (with a leading blank) for the format string, and think about gobbling input to end of line (or EOF if no EOL occurs before EOF) — int c; while ((c = getchar()) != EOF && c != '\n') ;. Commented Jun 27, 2017 at 19:29

2 Answers 2

1
char input;  
do
{
    displayWellDoneMenu();
    scanf("%c", &input);
    system("cls");
    if (c=='1' || c=='2')
       break;
    printf("You must select 1 or 2!\n"); 
}while(1);

switch (input)
{
   case'1':
       additionIntermediate();
      break;
   case '2':
       main();
       break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Obviously this is a true answer. I want to add a note, if you want assure that a block of code is executed Only one time use do... while (0), i see this way in some Cryptography projects. in some program control of execution may change to above that block and if you use while (0) method, execution will done only one time.
0

This could be an idea:

while(1){
    scanf("%c", &input);
    if(input == '1' || input == '0') break;
    // else continue cycle
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.