Reputation: 11
Here is a minimal form of the code emphasizing the issue I have. Code fails at "exit"(case '0') - program simply crashes. I suspect it is related to the while loop. The issue occurs no matter what character i choose for the exit case (instead of '0').
#include <stdio.h>
void main()
{
int run=1;
char menu_option;
while (run==1)
{
printf("Choose case:\n");
scanf ("%s", &menu_option);
switch (menu_option) {
case '1':
printf("1");
break;
case '2':
printf("2");
break;
case '0':
run=0;
break;
default:
printf("Wrong input, try again\n");
}
}
}
Upvotes: 0
Views: 67
Reputation: 93564
menu_option
is not a string, so %s
is the wrong format specifier. You need %c
, prefixed with a space to prevent whitespace (including newline) being interpreted as valid character input.
scanf (" %c", &menu_option);
Upvotes: 2