What's going on here? It seems that the program isn't waiting for me to input a character, but just skips right to the 2nd number. It's almost like it's taking the newline from num1, except i am using fflush(stdin) so it shouldn't be doing that. Notice that the 3 printf statements i put in there to show me what is going on, the one for operator comes up with a blank line meaning I think either space or new line... There may be problems with my if/else if, but I won't be able to work on it until my program deals with the operator first. I'd like to concentrate on that, even if you see a problem with my if/else if structure. Here is the program & output follows it:
Code:
#include <stdio.h>
int main (void)
{
/* declare variables */
/* ***************** */
float num1, num2, result;
char oper = '+';
/* display greeting */
/* and get info */
/* **************** */
printf("Welcome to the Acme Simple Calculator!\n");
printf("Enter a number: ");
scanf("%f", &num1);
fflush(stdin);
printf("num1 = %f\n", num1);
printf("Enter a math operation +, -, *, /: \n");
oper = getchar();
printf("The operator is %c", oper);
fflush(stdin);
printf("Enter a second number: ");
scanf("%f", &num2);
fflush(stdin);
printf("num2 = %f\n", num2);
/* calculate results */
/* & display results */
/* ***************** */
if ( oper = '+' )
result = num1 + num2;
else if ( oper = '-' )
result = num1 - num2;
else if ( oper = '*' )
result = num1 * num2;
else if ( oper = '/' )
result = num1 / num2;
else
printf("Invalid operator.\n");
return 0;
} /* end main */
[Session started at 2010-04-26 21:52:34 -0700.]
Welcome to the Acme Simple Calculator!
Enter a number: 3.5
num1 = 3.500000
Enter a math operation +, -, *, /:
The operator is
Enter a second number: 9.7
num2 = 9.700000
The Debugger has exited with status 0.