This is a homework problem for school. I would like some help and suggestions, possible point me in the right direction because i'm lost.
The homework assignment is to write 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, +,-,*,/,S,E. The S operator sets the "accumulator" to the typed in value and E exits. The program should check for division by zero and if the entered operator is not one of the valid operators.
This is what i have so far.
#include <stdio.h>
int main(void)
{
int exec = 1;
float sum = 0, num;
char oper;
printf("\nBegin Calculations \n");
while(exec == 1){
scanf("%f %c", &num, &oper);
switch(oper){
case ('+') : sum += num;
break;
case ('-') : sum -= num;
break;
case ('*') : sum = sum * num;
break;
case ('/') :
if(num==0){
printf("Cannot divide by zero");
break;
}
else{
sum = sum / num;
break;
}
case ('s') :
case ('S') : sum = num;
break;
case ('e') :
case ('E') :
printf("= %f \n", sum);
printf("End of Calculations");
exec = 0;
break;
default :
printf("Not a valid operator");
break;
}
printf("= %f \n", sum);
}
return 0;
}
Using gcc compiler on a Solaris workstation.
I get nothing when i run the program. This is my 4th revision of my code, I can't really get anything to work.
Thanks
The homework assignment is to write 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, +,-,*,/,S,E. The S operator sets the "accumulator" to the typed in value and E exits. The program should check for division by zero and if the entered operator is not one of the valid operators.
This is what i have so far.
#include <stdio.h>
int main(void)
{
int exec = 1;
float sum = 0, num;
char oper;
printf("\nBegin Calculations \n");
while(exec == 1){
scanf("%f %c", &num, &oper);
switch(oper){
case ('+') : sum += num;
break;
case ('-') : sum -= num;
break;
case ('*') : sum = sum * num;
break;
case ('/') :
if(num==0){
printf("Cannot divide by zero");
break;
}
else{
sum = sum / num;
break;
}
case ('s') :
case ('S') : sum = num;
break;
case ('e') :
case ('E') :
printf("= %f \n", sum);
printf("End of Calculations");
exec = 0;
break;
default :
printf("Not a valid operator");
break;
}
printf("= %f \n", sum);
}
return 0;
}
Using gcc compiler on a Solaris workstation.
I get nothing when i run the program. This is my 4th revision of my code, I can't really get anything to work.
Thanks