Why won't my code work- I don't understand! I read the other question that this was the "duplicate" of and understand the situation with "scanf". But in this situation, I provided a switch statement with a case where the variable would be white space, in which it would just exit the loop. This wouldn't cause problems as the loop will continue as long as the user doesn't input anything - it would just go through the loop 2 times (one for the user input, 1 for the whitespace).
#include <stdio.h>
int main(void) {
setbuf(stdout, 0);
int op = 0;
do{
int num1;
int num2;
printf("\n\nMenu of operations\n1. + \n2. - \n3. * \n4. / \n5. %%");
printf("\nEnter operation number to be executed: ");
op = getchar();
printf ("\nEnter first integer: ");
scanf ("%d", &num1);
printf ("Enter second integer: ");
scanf ("%d", &num2);
switch (op){
case '1':
printf ("\n%d + %d = %d", num1, num2, num1 + num2);
break;
case '2':
printf ("\n%d - %d = %d", num1, num2, num1 - num2);
break;
case '3':
printf ("\n%d * %d = %d", num1, num2, num1 * num2);
break;
case '4':
printf ("\n%d / %d = %f", num1, num2, (float)num1/num2);
break;
case '5':
printf ("\n%d %% %d = %d", num1, num2, num1%num2);
break;
case '\n':
case '\t':
case ' ' :
break;
default:
printf ("An invalid menu option");
break;
}
}while (op!=0);
return 0;
}
And the results are like this:
Menu of operations
1. +
2. -
3. *
4. /
5. %
Enter operation number to be executed: 4 //first time around- works fine
Enter first integer: 28
Enter second integer: 5
28 / 5 = 5.600000
Menu of operations
1. +
2. -
3. *
4. /
5. %
Menu of operations
1. +
2. -
3. *
4. /
5. %
Enter operation number to be executed:
Enter first integer: 4 //so one cannot type after the statement "Enter operation number to be executed:"
I understand that there's still a '\n' character in my buffer due to the enter which gets assigned to the op variable in the getchar() statement. But if that is the case, then the switch statement should take care of this- to be specific it should go to the case where the op variable is equal to '\n' or alternatively '\t', ' ' which I have written in my code.
case '\n':
case '\t':
case ' ' :
break;
This was how our lecturer taught us, but why won't this work in this case?
I found one more error I got that could possibly help find the source of this problem...
When I carry on with the second time inputting num1 and num2(where the 1st problem lies of not allowing me to input the variable op), the program gives this:
Enter operation number to be executed:
Enter first integer: 2
Enter second integer: 4
You may ask why that's a second problem, but in my code, I stated that if op was anything else other than 1,2,3,4,5,\n,\t, ' ', it should run what is under default and print "An invalid menu option."
However, it does neither of these things. Does anyone have an idea of what's going on?
Also, I tried doing what someone suggested in a comment- so I edited my code like this:
while (getchar() != '\n')
{
op = getchar();
}
However, this still doesn't solve the problem... I get the exact same result. Since the newline/enter isn't the problem here, does anyone know what is?