I have a simple calculator program, the program runs and I want it to say "calculator is on" but I only want it to say this "one time" at the beginning. I made a value count but it resets it self back to 0 at the beging of the loop when I return main() and start over. Can someone help me figure out what im doing wrong here.
btw to get the program to reset you need to enter 'r' in the terminal followed by 'y'
thanks so much for the help, my program seems pretty messy to me so if anyone has advice id love to here it.
Code:
#include <stdio.h>
double calculate(double currentVal, char operatorInput, double inputVal);
int main(){
int count = 0;
int done = 0;
double newResult = 0;
double inputVal;
char operatorInput;
char r, R;
char terminate;
char terminateB;
char terminateC;
printf("%d\n", count);
if (count <= 0) {
printf(" simple calculator \n");
printf("**********************************************************\n");
printf("Calculator is on.\n");
count++;
printf("%d\n", count);
}
printf("result = %.1f\n", newResult);
while (!done) {
scanf("%c", &operatorInput);
if (operatorInput == 'r' || operatorInput == 'R') {
EOF;
printf("final result is %.1f\n", newResult);
printf("agian? [y/n]\n");
getchar();
scanf("%c", &terminate, &terminateB, &terminateC );
if (terminate == 'y' || terminate == 'Y') {
getchar();
main();
}
else{
printf("end of program");
getchar();
return 0;
}
}
scanf("%lf", &inputVal);
getchar(); //flush buffer
if (operatorInput == '*' || operatorInput == '/' || operatorInput == '+' || operatorInput == '-') {
newResult = calculate(newResult, operatorInput, inputVal);
printf("result %c %.1lf = %.1lf\n", operatorInput, inputVal, newResult);
printf("new result = %.1f\n", newResult);
}
else {
printf("%c is an unknown operation\n", operatorInput );
printf("re-enter, your last line:\n");
}
}
}
double calculate(double currentVal, char operatorInput, double inputVal){
// a (x) b = c
double a, b, c;
char x;
a = currentVal;
x = operatorInput;
b = inputVal;
if (x == '*'){
c = a * b;
}
else if(x == '/'){
c = a / b;
}
else if(x == '+'){
c = a + b;
}
else if(x == '-'){
c = a - b;
}
return c;
}