Simple Multi-Session Calculator: Switch Case and While Loop Problem in C
Hello Everyone,
I have a problem with my simple operations calculator code (using C, in Code::Blocks). I am required to use a while loop statement so the user can execute multiple step operations without re-opening the program. When I launch the program, I get through the first session fine, but when I'm on the second session, when entering the two operands and press enter (to calculate), it just gives me the return and say press any key to continue (exit). You could run it, if it helps anything. I am so close, I've been working on this problem for 1.5 days, I'm sure it is something simple, please help! Thanks!:biggrin:
Here is the code:
Code:
# include <stdio.h>
int main()
{
int num1, num2;
char op;
int finished = 0;
printf("Simple Operations Calculator\n");
printf(" \n");
printf("Enter your desired operation (+, -, *, /): ");
scanf("%c", &op);
printf("Enter your two desired numbers: ");
scanf("%d%d", &num1, &num2);
switch(op)
{
case '+':
printf("num1 + num2 = %.2d\n", num1+num2);
break;
case '-':
printf("num1 - num2 = %.2d\n", num1-num2);
break;
case '*':
printf("num1 * num2 = %.2d\n", num1*num2);
break;
case '/':
printf("num1 / num2 = %.2d\n", num1/num2);
break;
default:
printf("Error! That is not an operation option.");
break;
}
while(!finished){
printf("Enter your desired operation (+, -, *, /): ");
scanf("%c", &op);
getchar();
printf("Enter your two desired numbers: ");
scanf("%d%d", &num1, &num2);
getchar();
return 0;
}
}