# Thread: Help About a simple calculator pls!!

1. ## Help About a simple calculator pls!!

hello all!! I really need a little help! My problem is that I am writing a simple calculator which is actually my homework and it must do calculation like 2*4+5 is 13..that is there is no precedence..But I dont know how I can write +5 after first equ( 2*4)..My program just gives 8 as a result..Pls help me!! what ı wrote::
Code:
```    int main(){
int a,b,sum,c;
char ch;
scanf("&#37;d%c%d",&a,&ch,&b);
while(ch!='!' ){
if(ch=='+')
sum=a+b;
if(ch=='-')
sum=a-b;
if(ch=='/')
sum=a/b;
if(ch=='*')
sum=a*b;

scanf("%c%d",&ch,&c);
printf("%d",sum);
anykey();
return 0;
}```

2. Read the first int, then everything after that is 'operation' and 'operand' pairs.

3. Why don't you assign sum = a before the while() loop? Then you can keep a running total each time through the loop:
Code:
```        if(ch=='+')
sum += b;
if(ch=='-')
sum -= b;
if(ch=='/')
sum /= b;
if(ch=='*')
sum *= b;```

4. Code:
```        if(ch=='+')
sum += b;
if(ch=='-')
sum -= b;
if(ch=='/')
sum /= b;
if(ch=='*')
sum *= b;```
Consider this. If you want an addition ( the first if ); the others if are still going to be checked for truthfulness. If else or switch are better alternatives.

5. Are you also considering in your program when the input of the user is not a single digit? what if it isdouble digit or more?

6. Originally Posted by aaron_88
hello all!! I really need a little help! My problem is that I am writing a simple calculator which is actually my homework and it must do calculation like 2*4+5 is 13..that is there is no precedence..But I dont know how I can write +5 after first equ( 2*4)..My program just gives 8 as a result..Pls help me!! what ı wrote::
Code:
```    int main(){
int a,b,sum,c;
char ch;
scanf("%d%c%d",&a,&ch,&b);
while(ch!='!' ){
if(ch=='+')
sum=a+b;
if(ch=='-')
sum=a-b;
if(ch=='/')
sum=a/b;
if(ch=='*')
sum=a*b;

scanf("%c%d",&ch,&c);
printf("%d",sum);
} /* maybe here? */
anykey();
return 0;
}```
I've highlighted the curly brace problem (a typo I suspect).
Your program logic doesn't DO anything if int c is added in. It just prints up the same sum