# Thread: need help with calculator code for c

1. ## need help with calculator code for c

I need to create a calculator that will read the operator first followed by a space the first number and then another space the second number. So the user input should look like this
+ 8 8
then output it like this
8 + 8 = 16
This is what i have so far, I was wondering if i was on the right track and if not some tips please.

insert
Code:
```#Include <stdio>
void main (void){

int sum, num1, num2, i;
double symbol;

printf("Enter Operator, Number 1, Number 2:\n");
scanf("%d",&symbol, &num1, &num2);

if (symbol = * ) {
for (i = 0; i < num1 ; i++){
sum = sum + num2;
}
printf("%d", num1, * , num2, = , sum);
}

else if (symbol = -){
num1 - num2 = sum;

printf("%d", num1, - , num2, =, sum);
}

else if (symbol = +){
num1 + num2 = sum;

printf("%d", num1, +, num2, =, sum);

}

else{
printf("Selection is Invalid");```

2. I think you may want to look up how C operators such as * + - / etc work in your c texts.
You have your assignments backwards and it's going to produce a ton of compiler errors.

3. char symbol;
scanf("%c %d %d", &symbol, &num1, &num2);
if (symbol == '*')
sum = num1 + num2

Lots of other minor issues.

4. Originally Posted by nonoob
Lots of other minor issues.
Are we on a roll today, or what... twice now we've answered within a minute of eachother...

5. Originally Posted by CommonTater
Are we on a roll today,
If you were butter maybe. ;-)

6. Originally Posted by CommonTater
Are we on a roll today, or what... twice now we've answered within a minute of eachother...
Slow day at work.

7. Originally Posted by nonoob
Slow day at work.
Currently between projects...

8. okay so i changed somethings around...will this work or do i have to make the options cases?

insert
Code:
```#Include <stdio>
void main (void){

int sum, num1, num2, i;
char symbol;

printf("Enter Operator, Number 1, Number 2:\n");
scanf("%c %d %d",&symbol, &num1, &num2);

if (symbol== '*' ) {
for (i = 0; i < num1 ; i++){
sum = sum + num2;
}
printf("%d", num1, * , num2, = , sum);
}

else if (symbol== '-'){
num1 - num2 = sum;

printf("%d", num1, - , num2, =, sum);
}

else if (symbol== '+'){
num1 + num2 = sum;

printf("%d", num1, +, num2, =, sum);

}
else if (symbol == '/'){

}

else if (symbol== 'e'){

}

else{
printf("Selection is Invalid");

}

}```

9. First of all... have you tried compiling your code? What errors did you get?

C is case sensitive... #Include will not work... #include will.
C knows how to multiply... for your multiply function just use sum = num1 * num2;

10. Code:
`num1 + num2 = sum;`
There's no way you've compiled with stuff like this.

11. Originally Posted by mike65535
Code:
`num1 + num2 = sum;`
There's no way you've compiled with stuff like this.
That was sort of my point... he's gonna get a whole mess of errors and hopefully he'll climb into the books and learn some about C.