Still learning C, playing with if, then, else. In the program below, the final ELSE statement executes in every WHILE loop. I'm not sure why. Program and output are below. I was thinking that at each getchar() in the WHILE loop, the program would pause, take in the input and then bounce back up to the WHILE evaluation. Thanks for your help.
Jim
Code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
char x;
printf("Enter a, b or c (q to quit);\n");
x = getchar();
while (x != 'q') {
if (x=='a') {
printf("Menu A\n");
x = getchar();
}
else if (x=='b'){
printf("Menu B\n");
x = getchar();
}
else if (x=='c'){
printf("Menu C\n");
x = getchar();
}
else {
printf("Not a valid option\n");
x = getchar();
}
}
return(0);
}
And the output is:
Running…
Enter a, b or c (q to quit);
a
Menu A
Not a valid option
b
Menu B
Not a valid option
d
Not a valid option
Not a valid option
q
Debugger stopped.
Program exited with status value:0.