'Multiple choice menu program in C using the switch statement and scanf () function
I'm trying to write a program with choice menu using the switch statement in C. Here's my code:
main ()
{
char option;
int test;
start:
printf ("Enter: ");
scanf ("%c", &option);
while (getchar() != '\n');
switch (option)
{
case '1':
printf ("Test 1 : ");
scanf ("%d", &test);
break;
case '2':
printf ("Test 2 : ");
break;
}
if (option != 'q') goto start;
}
The program is intended to repeat until 'q' character is inputted from the keyboard. The problem is whenever I try to input '1' from the keyboard for the first time (which executed the case 1), the next time I input '1' or '2' the program will skip case 1 (or 2) and go directly to the next loop, however after the next loop it executed the case 1 (or 2) normally. Besides, the case 2 (which don't have the scanf () function) everthing work correctly. I have also tried to remove the scanf from the case 1 and after that case 1 executed normally. Here're some of the output:
Enter: 1
Test 1 : 5
Enter: 1
Enter: 1
Test 1 : 7
Enter: 2
Enter: 2
Test 2 :
Enter: 2
Test 2 :
Can anyone explain to me what's wrong with my code and tell me how to fix it?
Solution 1:[1]
Instead of goto use a loop -
while(option!='q'){
printf ("Enter: ");
if(scanf (" %c", &option)==1){ // leave a space before %c and check return of scanf
//while (getchar() != '\n');
switch (option)
{
case '1':
printf ("Test 1 : ");
scanf ("%d", &test);
break;
case '2':
printf ("Test 2 : ");
break;
}
}
}
Solution 2:[2]
The scanf ("%d", &test) under case1 is invoked when the current character in stdin is the first character of the next line (this happens after while (getchar() != '\n') is done. scanf ("%d", &test) 'eats' this character (that is probably expected to specify the next line option kind) and due to this fact the call to scanf ("%c", &option) happening in the next loop iteration no more can 'see' that character (it 'sees' the next one instead).
Solution 3:[3]
Going through your code:
scanf ("%c", &option);
you type 1\n (\n is the newline character generated on pressing Enter). The above scanf consumes 1, leaving \n in the standard input stream(stdin). Next,
while (getchar() != '\n');
consumes the \n and breaks out of the loop (as the condition is false). Now,
scanf ("%d", &test);
you type <number>\n (<number> is the number you input). The above scanf consumes <number> leaving \n in the stdin. After the goto gets executed, execution returns back to
scanf ("%c", &option);
This scanf sees the \n in the stdin left over by the previous call to scanf, and consumes it, and thus, does not wait for further input. This is the problem.
Fixes for it include changing
This:
scanf ("%d", &test);to
scanf ("%d%*c", &test); /* `%*c` scans and discards a character */This
scanf ("%c", &option);to
scanf (" %c", &option); /* The space discards all whitespace characters, if any, until the first non-whitespace character */- Using
fgetsto scan a line andsscanfto parse the data accordingly
etc.
Solution 4:[4]
Please consider I only started 3 days ago.
very easy solution I came up with is this one:
#include<stdio.h>
int main() {
char i,a,b;
a='a';
b='b';
printf("choose a greeting: for welcome type a, for hello type b:");
scanf("%c" ,&i);
switch(i) {
case 'b':
printf("hello");
break;
case 'a':
printf("welcome");
break;
}
printf(" Mr. Bigotes");
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ameyCU |
| Solution 2 | AndreyS Scherbakov |
| Solution 3 | Spikatrix |
| Solution 4 |
