# Printing calculator - Stephen Kochan exercise

I'm a newbie at programming, just one month into C. Working with Stephen Kochan's programming in C. There is a question in chapter 6 to write a printing calculator where 's' tells the program to set the accumulator, 'e' tells that the execution must end. And the operators tell the operations to be performed on the accumulations.

The sample run according to the book looks like this:

10 s
=10.000000
2 /
=5.000000
55 -
=-50.000000
100.25 S
=100.250000
0 E
=100.250000


I got this to work but I'm not sure about how correct this code is.

#include<stdio.h>
// write a program for a printing calculator
// exercise 4, chapter 6, kochan

int main(void)
{
float value, output;
char operator;
_Bool exit;

printf("Please enter a value and operator to begin\n");
scanf("%f %c", &value, &operator);

while(operator != 'e' || operator != 'E')
{
exit = 0;

switch(operator)
{
case 'S':
case 's':
output = value;
printf("= %.3f\n", output);
scanf("%f  %c", &value, &operator);
break;

case '+':
output = output + value;
printf("= %3f\n", output);
scanf("%f  %c", &value, &operator);
break;

case'-':
output = output - value;
printf("= %.3f\n", output);
scanf("%f  %c", &value, &operator);
break;

case '*':
output = output * value;
printf("= %.3f\n", output);
scanf("%f  %c", &value, &operator);
break;

case '/':
if(value == 0)
printf("Division by zero\n");
else
output = output / value;
printf("= %.3f\n", output);
scanf("%f  %c", &value, &operator);
break;

case 'E':
case 'e':
output = output + value;
printf("= %.3f\n", output);
exit = 1;
return 0;

default:
printf("Unidentified operator\n");
return 0;

}
}

return 0;
}

• Does your code work as expected, @Divya? – Dan Mar 10 '15 at 11:57
• it does work as expected but i had errors i reworked it..the float were set incorrectly for print..there were repetitions.. – Divya Mathur Mar 16 '15 at 9:03

## 1 Answer

This seems correct, and it works. Some simplifications are possible.

### Don't repeat yourself

These lines appear many times in the code:

printf("= %.3f\n", output);
scanf("%f  %c", &value, &operator);


You can move this part after the switch block to reduce duplication.

### Remove unnecessary elements

The exit variable is written to but never read. It seems unused, so I suggest to remove it.

### Coding style

It would be easier to read if you indent the body of case statements.

It's recommended to use braces with all if-else statements.

I suggest to put a space after #include and before <stdio.h>

### Suggested implementation

Putting together the suggestions above, your implementation becomes simpler and easier to read:

#include <stdio.h>

int main(void)
{
float value, output;
char operator;

printf("Please enter a value and operator to begin\n");
scanf("%f %c", &value, &operator);

while (operator != 'e' || operator != 'E')
{
switch(operator)
{
case 'S':
case 's':
output = value;
break;

case '+':
output += value;
break;

case'-':
output -= value;
break;

case '*':
output *= value;
break;

case '/':
if (value == 0)
{
printf("Division by zero\n");
}
else
{
output /= value;
}
break;

case 'E':
case 'e':
output += value;
printf("= %.3f\n", output);
return 0;

default:
printf("Unidentified operator\n");
return 0;
}
printf("= %.3f\n", output);
scanf("%f %c", &value, &operator);
}

return 0;
}

• thank you so much.. i worked this out and the output is as desired.. – Divya Mathur Mar 16 '15 at 9:01
• @DivyaMathur if this helped, then perhaps you can accept it ;-) – janos Apr 22 '15 at 6:41