Hey I'm suppose to make a calculator for one of my assignments I'm just having this annoying problem and can't get it fixed.
I'm trying to use and if-else statement but the else portion keeps executing regardless. please help. I'm not asking you to do my homework for me just correct syntax.
thanks
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
double main(void)
{
double result = 0.0, num1; char operator, end;
printf("Calculator is on.\n\n");
do {
printf("Enter an operator symbol:\n");
scanf("%c",&operator);
if(operator == '+'){
scanf("%lf",&num1);
result = result + num1;
printf("%lf",result);}
if(operator == '-'){
scanf("%lf",&num1);
result = result - num1;
printf("%lf",result);}
if(operator == '*'){
scanf("%lf",&num1);
result = result * num1;
printf("%lf",result);}
if(operator == '/'){
scanf("%lf",&num1);
result = result / num1;
printf("%lf",result);}
else{
printf("UnknownOperatorException is thrown\n");}
}while(end!='r' ||end!='R');
return 0;
}
Also would i have to put a getchar statement for the while statement to end or can i just move down the while statement after the scanf statement and input 'operator' instead of 'end'