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;
}
Xin Wei
  • 37
  • 1
  • 6
  • 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'` – lurker Jun 27 '17 at 14:45
  • @lurker I see the problem now, thanks!! – Xin Wei Jun 27 '17 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. – lurker Jun 27 '17 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. – chux - Reinstate Monica Jun 27 '17 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)`](http://stackoverflow.com/questions/2979209/using-fflushstdin), 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') ;`. – Jonathan Leffler Jun 27 '17 at 19:29

2 Answers2

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;
}
Ivan Sheihets
  • 802
  • 8
  • 17
  • 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. – EsmaeelE Jun 27 '17 at 15:15
0

This could be an idea:

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