I was doing an exercise of the book Programming in C 4th edition, This is the exercise

``````/*
* 4. Write a program that acts as a simple “printing” calculator. The program
* should allow the user to type in expressions of the form
*
*		number   operator
*
* The following operators should be recognized by the program:
*
*		+     -     *     /     S     E
*
* The S operator tells the program to set the “accumulator” to the typed-in
* number. The E operator tells the program that execution is to end. The
* arithmetic operations are performed on the contents of the accumulator with
* the number that was keyed in acting as the second operand. The following is
* a “sample run” showing how the program should operate:
*
*		Begin Calculations
*		10 S           Set Accumulator to 10
*		= 10.000000    Contents of Accumulator
*		2 /            Divide by 2
*		= 5.000000     Contents of Accumulator
*		55 -           Subtract 55
*		-50.000000
*		100.25 S       Set Accumulator to 100.25
*		= 100.250000
*		4 *            Multiply by 4
*		= 401.000000
*		0 E            End of program
*		= 401.000000
*		End of Calculations.
*
* Make certain that the program detects division by zero and also checks for
* unknown operators.
*
*/

``````

And for some reason when i try to exit typing E, neither my code or the code of a solution that i found on the internet worked, it always end in an infinite loop.
This is the solution i found on the internet:

``````#include <stdio.h>

int main(void)
{
float number, accumulator;
char operator;

printf("Begin Calculations\n\n");
scanf("%f %c", &number, &operator);

accumulator = 0;
while (operator != 'E') {
switch (operator) {
case 'S':
printf("= %f\n\n", accumulator = number);
break;
case '+':
printf("= %f\n\n", accumulator += number);
break;
case '-':
printf("= %f\n\n", accumulator -= number);
break;
case '*':
printf("= %f\n\n", accumulator *= number);
break;
case '/':
if (number == 0)
printf("Division by zero\n\n");
else
printf("= %f\n\n", accumulator /= number);
break;
default:
printf("Uknown operator\n\n");
break;
}
scanf("%f %c", &number, &operator);
}

printf("= %f\n\n", accumulator);
printf("End of Calculations\n\n");

return 0;
}
``````

This is my solution:

``````#include <stdio.h>

int main(void){
float accumulator = 0, number;
char operator;

printf("Begin Calculations\n");
scanf("%f %c", &number, &operator);

while (operator != 'E'){
switch (operator){
case 'S':
if (accumulator != 0)
accumulator = number;
else
accumulator += number;
scanf("%f %c", &number, &operator);
break;
case '*':
accumulator *= number;
printf("= %f\n", accumulator);
scanf("%f %c", &number, &operator);
break;
case '/':
if (number == 0)
printf("Cannot divide by 0!");
else
accumulator /= number;
printf("= %f\n", accumulator);
scanf("%f %c", &number, &operator);
break;
case '+':
accumulator += number;
printf("= %f\n", accumulator);
scanf("%f %c", &number, &operator);
break;
case '-':
accumulator -= number;
printf("= %f\n", accumulator);
scanf("%f %c", &number, &operator);
break;
case 'E':
break;
default:
printf("Unkown operator\n");
scanf("%f %c", &number, &operator);
break;
}
}
printf("= %f\n", accumulator);
printf("End of Calculations\n");

return 0;
}
``````

I’m not sure if this is the right place for questions like this that are not related to Exercism. But I’ll give it a go:

Did you by any chance try to end the calculation by entering a single `E` without a number?

The function call `scanf("%f %c", ...)` is very specific about what the program should read: First a floating point value, then some optional whitespace, then a single character.

When you enter `E` alone the `scanf("%f %c", ...)` sees the `'E'` and cannot process it because it expects a floating point value first. It leaves the `'E'` in the input stream and returns (with the return value `0`). The `number` and `operator` do not get modified, they keep their old values, and the next iteration of the `while` loop effectively repeats the previous operation.
Then `scanf(%f %c", ...)` gets executed again, cannot process the `'E'` that’s still present in the input stream, returns and leaves `number` and `operator` unmodified. The `while loop` repeats the previous operation.
Then `scanf(%f %c", ...)` gets executed again, cannot process the `'E'` that’s still present in the input stream, returns and leaves `number` and `operator` unmodified. The `while loop` repeats the previous operation.
That’s an infinite loop.

If you want to end the calculation you have to enter a number and then the letter `E`. You can see that in the example.

1 Like