When you run this code, it somehow works as well as the number of characters entered by scanf after the first operation. What's the problem?
I filled in the blank space before %c because I thought it might be a buffer problem, but the error still occurs.
void EX18() {
float x, y;
char op;
int i = 0;
while (1) {
/*
printf("\n******************** \n");
printf("A ---- Add \n");
printf("S ---- Subtract \n");
printf("M ---- Multiply \n");
printf("D ---- Divide \n");
printf("Q ---- Quit \n");
printf("******************** \n");
*/
i++;
printf("\n%dth iteration \n",i);
printf("Select an operation: ");
scanf(" %c", &op);
if (op == 'Q') break;
else {
switch (op) {
case 'A':
printf("Enter two numbers separated by a space: ");
scanf("%f %f", &x, &y);
printf("%.2f \n", x + y);
break;
case 'S':
printf("Enter two numbers separated by a space: ");
scanf("%f %f", &x, &y);
printf("%.2f \n", x - y);
break;
case 'M':
printf("Enter two numbers separated by a space: ");
scanf("%f %f", &x, &y);
printf("%.2f \n", x * y);
break;
case 'D':
printf("Enter two numbers separated by a space: ");
scanf("%f %f", &x, &y);
printf("%.2f \n", x / y);
break;
default:
printf("\nUnknown command. Please try again.\n");
}
}
}
}
int main()
{
EX18();
//TEST5();
return 0;
}
Result Screen :
1th iteration Select an operation: 00
Unknown command. Please try again.
2th iteration Select an operation: Unknown command. Please try again.
3th iteration Select an operation: 000
Unknown command. Please try again.
4th iteration Select an operation: Unknown command. Please try again.
5th iteration Select an operation: Unknown command. Please try again.
6th iteration Select an operation: