Thread: Help with Basic Calculator Program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    21

    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;
        printf("Calculator Menu\n1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n\nYour Choice: ");
        scanf ("%d", &choice);
        switch(choice){
        case 1:
         printf("\nAddition\nPlease Enter The Numbers You Want To Add\n");
        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. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How about you give us some constructive information about how it's messing up....

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    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. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    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.
    Last edited by doilin; 12-08-2011 at 07:47 PM.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by doilin View Post
    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.
    Last edited by Mark Labarbara; 12-08-2011 at 07:58 PM.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    ^ 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)
    Last edited by doilin; 12-08-2011 at 08:05 PM.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    Quote Originally Posted by Mark Labarbara View Post
    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(){
        
    printf("Please Enter Your Equation: ");
    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
    Last edited by anonymoususer; 12-08-2011 at 08:33 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by doilin View Post
    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. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anonymoususer View Post
    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(){
        
    printf("Please Enter Your Equation: ");
    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. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Okay, I'll give suggestions next time. I didn't realize that people would get angry over something like this!

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by doilin View Post
    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. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by anonymoususer View Post
    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(){
        
    printf("Please Enter Your Equation: ");
    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. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by doilin View Post
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Calculator Program
    By Interista in forum C Programming
    Replies: 7
    Last Post: 10-12-2011, 10:34 AM
  2. Basic calculator
    By Khaled in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2011, 08:17 AM
  3. Basic Sum Calculator
    By ScoutDavid in forum C Programming
    Replies: 14
    Last Post: 09-26-2010, 02:01 AM
  4. Basic Calculator
    By Surge in forum C Programming
    Replies: 18
    Last Post: 12-02-2006, 10:20 PM
  5. Basic calculator program in C++
    By linkofazeroth in forum C++ Programming
    Replies: 70
    Last Post: 08-28-2005, 04:23 PM