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;
}