# Thread: Help with Basic Calculator Program

1. ## Help with Basic Calculator Program

Can I get some constructive criticism on why my program doesnt calculate properly?

Code:
```#include <stdlib.h>
#include <stdio.h>

int main(){
int sum;
int subtract;
int multiply;
int divide;
int choice;
int num1;
int num2;
sum=num1+num2;
subtract=num1-num2;
multiply=num1*num2;
divide=num1/num2;
scanf ("%d", &choice);
switch(choice){
case 1:
printf("Number 1: ");
scanf ("%d",&num1 );
printf("Number 2: ");
scanf ("%d",&num2 );
printf("The Sum of %d + %d = %d", num1, num2, sum);
case 2:
printf("\nSubtraction\nPlease Enter The Numbers You Want To Subtract\n");
printf("Number 1: ");
scanf ("%d",&num1 );
printf("Number 2: ");
scanf ("%d",&num2 );
printf("The Difference of %d + %d = %d", num1, num2, subtract);
case 3:
printf("\nMultiplication\nPlease Enter The Numbers You Want To Multiply\n");
printf("Number 1: ");
scanf ("%d",&num1 );
printf("Number 2: ");
scanf ("%d",&num2 );
printf("The Multiplication of %d + %d = %d", num1, num2, multiply);
case 4:
printf("\nDivision\nPlease Enter The Numbers You Want To Divide");
printf("Number 1: ");
scanf ("%d",&num1 );
printf("Number 2: ");
scanf ("%d",&num2 );
printf("The Division of %d + %d = %d", num1, num2, divide);
}
system("pause");

}```

2. How about you give us some constructive information about how it's messing up....

3. The reason it will not calculate properly is because you have a SIGFPE (Floating Point Exception), and therefore will not run. GDB calls it an Arithmetic exception.

Read up on SIGFPE here to figure out how to fix it.

4. That is too complicated. It uses way too many unnecessary variables. I was able to do the same thing with this:
Code:
```#include <stdio.h> // This line includes a file that allows basic input and output operations.

int main() { // Everything inside this compound statement will be executed by the program.
float input = 0.0; // This line creates a floating-point variable that holds a value input by the user.
float operation = 0.0; // This line creates a floating-point variable that will be added to 'input'.
float result = 0.0; // This line creates a floating-point variable that holds the result of the operation between 'input' and 'operation'.
char type; // This creates a character variable that holds the sign for a mathematical operation.
printf("Input the value that you plan to perform calculations on. "); // This line asks for a value that will be calculated upon.
scanf("%f", &input); // This actually performs the process of getting the number from the keyboard.
printf("Input the value that you plan to use in the calculation. "); // This line asks for a value that will be added to 'input'.
scanf("%f", &operation); // This actually performs the process of getting the number from the keyboard.
printf("Input the sign of the operation you plan to do. "); // This line asks for the symbol of a mathematical operation.
scanf(" %c", &type); // This actually performs the process of getting the symbol from the keyboard.
if(type == '+') { // This will be performed if the variable 'type' is equal to '+'.
result = input + operation; } // This sets 'result' equal to addition between 'input' and 'operation'.
if(type == '-') { // This will be performed if the variable 'type' is equal to '-'.
result = input - operation; } // This sets 'result' equal to subtraction between 'input' and 'operation'.
if(type == '*') { // This will be performed if the variable 'type' is equal to '*'.
result = input * operation; } // This sets 'result' equal to multipication between 'input' and 'operation'.
if(type == '/') { // This will be performed if the variable 'type' is equal to '/'.
result = input / operation; } // This sets 'result' equal to division between 'input' and 'operation'.
printf("The answer is: %f", result); // This prints the answer after the operations have been performed.
printf("\n"); // This makes the output look correct in the console, since it doesn't automatically advance to the next line.
return 0; } // This line returns the value '0' to the system, which means that it went well.```

5. Originally Posted by doilin
That is too complicated. It uses way too many unnecessary variables. I was able to do the same thing with this:
Spoon feeding him answers doesn't really help him learn.

6. ^ Eh, I'm new here. I commented the whole thing, if it helps... :/

(what's weird is I had just finished that code before looking at this topic)

7. Originally Posted by Mark Labarbara
Spoon feeding him answers doesn't really help him learn.
Nobody is spoon feeding me. I'm just learning to code better. Thats why I asked for constructive criticism. Thanks Doilin

Code:
```#include <stdio.h>
#include <stdlib.h>

int num1;
int num2;
int final;
char sign[1];
int main(){

scanf ("%d %c %d", &num1, &sign, &num2);
if (*sign == '+'){
final= num1+num2;
}
else if (*sign == '-'){
final= num1-num2;
}
else if (*sign == '*'){
final= num1*num2;
}
else if (*sign == '/'){
final= num1/num2;

}//end if
printf("%d", final);
system ("Pause");
}//End Main Function```

8. Originally Posted by doilin
That is too complicated. It uses way too many unnecessary variables. I was able to do the same thing with this:
Code:
```#include <stdio.h> // This line includes a file that allows basic input and output operations.

int main() { // Everything inside this compound statement will be executed by the program.
float input = 0.0; // This line creates a floating-point variable that holds a value input by the user.
float operation = 0.0; // This line creates a floating-point variable that will be added to 'input'.
float result = 0.0; // This line creates a floating-point variable that holds the result of the operation between 'input' and 'operation'.
char type; // This creates a character variable that holds the sign for a mathematical operation.
printf("Input the value that you plan to perform calculations on. "); // This line asks for a value that will be calculated upon.
scanf("%f", &input); // This actually performs the process of getting the number from the keyboard.
printf("Input the value that you plan to use in the calculation. "); // This line asks for a value that will be added to 'input'.
scanf("%f", &operation); // This actually performs the process of getting the number from the keyboard.
printf("Input the sign of the operation you plan to do. "); // This line asks for the symbol of a mathematical operation.
scanf(" %c", &type); // This actually performs the process of getting the symbol from the keyboard.
if(type == '+') { // This will be performed if the variable 'type' is equal to '+'.
result = input + operation; } // This sets 'result' equal to addition between 'input' and 'operation'.
if(type == '-') { // This will be performed if the variable 'type' is equal to '-'.
result = input - operation; } // This sets 'result' equal to subtraction between 'input' and 'operation'.
if(type == '*') { // This will be performed if the variable 'type' is equal to '*'.
result = input * operation; } // This sets 'result' equal to multipication between 'input' and 'operation'.
if(type == '/') { // This will be performed if the variable 'type' is equal to '/'.
result = input / operation; } // This sets 'result' equal to division between 'input' and 'operation'.
printf("The answer is: %f", result); // This prints the answer after the operations have been performed.
printf("\n"); // This makes the output look correct in the console, since it doesn't automatically advance to the next line.
return 0; } // This line returns the value '0' to the system, which means that it went well.```
And what did he learn from this mess?

Handing people cut and paste solutions teaches them nothing and you should not do it.

Sure you put a smile on his face, but he's no better programmer for it.

9. Originally Posted by anonymoususer
Nobody is spoon feeding me. I'm just learning to code better. Thats why I asked for constructive criticism. Thanks Doilin

Code:
```#include <stdio.h>
#include <stdlib.h>

int num1;
int num2;
int final;
char sign[1];
int main(){

scanf ("%d %c %d", &num1, &sign, &num2);
if (*sign == '+'){
final= num1+num2;
}
else if (*sign == '-'){
final= num1-num2;
}
else if (*sign == '*'){
final= num1*num2;
}
else if (*sign == '/'){
final= num1/num2;

}//end if
printf("%d", final);
system ("Pause");
}//End Main Function```
What you've got now isn't going to work either.

Line 8 ... an array with a size of 1 is a single variable... lose the [] stuff.
Line 13, 16, 19, 22 ... sign is not a pointer.
line 29... programs return an integer errorlevel to the operating system. You returned nothing.

10. Okay, I'll give suggestions next time. I didn't realize that people would get angry over something like this!

11. Originally Posted by doilin
Okay, I'll give suggestions next time. I didn't realize that people would get angry over something like this!

Its not anger, friend. People just want others to learn and think for themselves, that way the can become better programmers. That is all

12. Originally Posted by anonymoususer
Nobody is spoon feeding me. I'm just learning to code better. Thats why I asked for constructive criticism. Thanks Doilin

Code:
```#include <stdio.h>
#include <stdlib.h>

int num1;
int num2;
int final;
char sign[1];
int main(){

scanf ("%d %c %d", &num1, &sign, &num2);
if (*sign == '+'){
final= num1+num2;
}
else if (*sign == '-'){
final= num1-num2;
}
else if (*sign == '*'){
final= num1*num2;
}
else if (*sign == '/'){
final= num1/num2;

}//end if
printf("%d", final);
system ("Pause");
}//End Main Function```
As CommonTater said, that won't work either. If you look at the link I provided you should see how you can fix your original code with about 5 edits.

13. Originally Posted by doilin
Okay, I'll give suggestions next time. I didn't realize that people would get angry over something like this!
This isn't a free software service and you're not helping anyone by handing out ready to use solutions....
Think how you learned your skills... why would you deny that opportunity to anyone else?