I have tried to write my first calculator, and found some examples online, which I then changed to make them easier in terms of flow. However when I change the flow from this:
#include <stdio.h>
main()
{
char operator;
float num1,num2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c" ,&operator);
printf("Enter first operand: ");
scanf("%f" ,&num1);
printf("Enter second operand: ");
scanf("%f" ,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f\n" ,num1+num2);
break;
case '-':
printf("num1-num2=%.2f\n" ,num1-num2);
break;
case '*':
printf("num1*num2=%.2f\n" ,num1*num2);
break;
case '/':
printf("num1/num2=%.2f\n" ,num1/num2);
break;
default: //of operator is other than +, -, *, /, erros message shown
printf("Error! Invalid operator, this is basic math only.\n");
}
return 0;
}
to this:
#include <stdio.h>
main()
{
char operator;
float num1,num2;
printf("Enter first operand: ");
scanf("%f" ,&num1);
printf("Enter an operator (+, -, *, /): ");
scanf("%c" ,&operator);
printf("Enter second operand: ");
scanf("%f" ,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f\n" ,num1+num2);
break;
case '-':
printf("num1-num2=%.2f\n" ,num1-num2);
break;
case '*':
printf("num1*num2=%.2f\n" ,num1*num2);
break;
case '/':
printf("num1/num2=%.2f\n" ,num1/num2);
break;
default: //of operator is other than +, -, *, /, erros message shown
printf("Error! Invalid operator, this is basic math only.\n");
}
return 0;
}
basically changed the flow from: enter operator, then enter first number, then second number. To: enter first number, then enter operator, then enter second number. My problem is when I do this, I see the Enter operator, but the program skips over the option to enter the operator and asks for: enter first number then enter second number. The response is the default switch.