Hey guys, this is my first post so bear with me here. I decided to learn how to program in C and have been working through "Programming in C" by Stephen G. Kochan, though I've hit a snag on one of the problems and no one has been able to help me yet. For those who are familiar with the book it is Chapter 6, exercise 4: write a simple printing calculator that takes an accumulator and performs operations with inputted numbers and operators.
The program I've written compiles correctly and does all the math correctly, but for some reason it skips over the first print statement and then every print output is one step behind. So for instance if I do 10 S, 5 *, the output will be nothing after the 10 S, then 10.000000 after the 5 *, etc. I've tried moving the printf to different areas in the program and the output is always the same. Here is the code:
Code:
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
float number = 0.0, accumulator = 0.0;
char operator;
bool end_flag = false;
printf ("Please enter calculations:\n");
while ( end_flag == false ) {
scanf ("%f %c\n", &number, &operator);
switch (operator)
{
case 'S':
accumulator = number;
break;
case '+':
accumulator += number;
break;
case '-':
accumulator -= number;
break;
case '*':
accumulator *= number;
break;
case '/':
accumulator /= number;
break;
case 'E':
end_flag = true;
break;
default:
printf ("Unknown operator.\n");
break;
}
printf ("= %f\n", accumulator);
}
return 0;
}