simple calculator problem...

This is a discussion on simple calculator problem... within the C Programming forums, part of the General Programming Boards category; below is my calculator programming.I want to make it show "maths error" when any number divided by 0, but i ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    14

    simple calculator problem...

    below is my calculator programming.I want to make it show "maths error" when any number divided by 0, but i get any number +,-,*,/ by 0 also get maths error.I need u guys to correct my program,please.
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        float num1, num2;
        char operation;
        while (1) {
            scanf("%f%c%f", &num1, &operation, &num2);
            if (operation == '+')
                printf("%f\n", num1+num2);
            if (operation == '-')
                printf("%f\n", num1-num2);
            if(operation == '*')
                printf("%f\n", num1*num2);
            if (num2 == 0)
            {
                printf ("maths error\n");
            }
            else if (operation == '/')
                printf("%f\n", num1/num2);
        }
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Think: this maths error only applies if operation is equal to '/'.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    You need to look at how the if statements are evaluated. The way your code looks now, it will always check if num2 is equal to zero, even if you are using the +, - or * operator.

    What you want to do is probably something like:
    Code:
    if (operation == '+') {
        /* do + stuff */
    } else if (...) {
        /* .... */
    } else if (operation == '/') {
        /* check for division by zero */
    }
    If you don't want all those if-statements lining up, you could look into using a switch-statement instead for checking the operator.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    iceaway, thanks so much!i got it!
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        float num1, num2;
        char operation;
        while (1) {
            scanf("%f%c%f", &num1, &operation, &num2);
            if (operation == '+')
                printf("%f\n", num1+num2);
            else if (operation == '-')
                printf("%f\n", num1-num2);
            else if(operation == '*')
                printf("%f\n", num1*num2);
            else if (num2 == 0)
            {
                printf ("maths error\n");
            }
            else if (operation == '/')
                printf("%f\n", num1/num2);
        }
        return 0;
    }
    now i want my calculator to show in 2 decimal place, what command should i put?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Although that it logically correct, it would be easier to understand if you wrote it in this way:
    Code:
    if (operation == '+')
        printf("%f\n", num1+num2);
    else if (operation == '-')
        printf("%f\n", num1-num2);
    else if(operation == '*')
        printf("%f\n", num1*num2);
    else if (operation == '/')
    {
        if (num2 == 0)
        {
            printf ("maths error\n");
        }
        else
        {
            printf("%f\n", num1/num2);
        }
    }
    Quote Originally Posted by yeohwl91
    now i want my calculator to show in 2 decimal place, what command should i put?
    Read up on printf, in particular, how to modify the format string to format such numbers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    help me to check whether my flowchart have problem?
    thanks...

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        float num1, num2;
        char operation;
        while (1) {
            printf ("Enter Calculation:\n");
            scanf("%f%c%f", &num1, &operation, &num2);
            if (operation == '+')
                printf("%.2f%c%.2f=%.2f\n",num1,operation,num2, num1+num2);
            else if (operation == '-')
                printf("%.2f%c%.2f=%.2f\n",num1,operation,num2, num1-num2);
            else if(operation == '*')
                printf("%.2f%c%.2f=%.2f\n",num1,operation,num2, num1*num2);
            else if (num2 == 0)
            {
                printf ("maths error\n");
            }
            else if (operation == '/')
                printf("%.2f%c%.2f=%.2f\n",num1,operation,num2, num1/num2);
        }
        return 0;
    }
    Attached Images Attached Images  
    Last edited by yeohwl91; 09-26-2011 at 06:53 AM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Can't, it's too small to read.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    i have no idea how to make it bigger, because i just print screen from the microsoft word. please download below attachment and help me check.

    Attachment 10913

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    i not really understand what is pseudo code, but i try to write it. Help me check whether correct or not, thanks a lot.
    Code:
    input (mathematical expression)
    if calculation is not division
    then go to the calculating and round off value into 2 decimal places
    if calculation is division
    then check whether denominator is zero
    else if denominator is zero
    then show "maths error"
    else if denominator is not zero
    then go to the calculating and round off value into 2 decimal places
    if end
    output (show answer)
    Last edited by yeohwl91; 09-26-2011 at 09:17 AM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your attachment attempt #2 doesn't work "Invalid attachment".

    Your pseudo code should have indentation (like your code should have), on all subordinate lines of code.

    The amount that a line is indented from a previous line should never by more than 2 to 5 spaces. (and spaces work more reliably on the board, then tabs do).

    code tags - use 'em always for code and pseudo code. highlight your code, and in the advanced editor, click on the # icon near the upper right hand corner of the editor.

    [code ]
    Code:
     
    if(you don't have code tags and indentation)
       printf("Your code will be hard to read/study\n");
    [/code ]

    Code tags are just like the what you see above EXCEPT they don't have a space after the e.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    Code:
    scanf (mathematical expression)
    if (calculation is not division)then (go to the calculating and round off value into 2 decimal places)
    if (calculation is division)
    then (check whether denominator is zero)
    else if (denominator is zero)
    then (show "maths error")
    else if (denominator is not zero)
    then (go to the calculating and round off value into 2 decimal places)
    if end
    printf (show answer)
    Last edited by yeohwl91; 09-26-2011 at 09:38 AM.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by yeohwl91 View Post
    Code:
    scanf (mathematical expression)
    if (calculation is not division)then (go to the calculating and round off value into 2 decimal places)
    if (calculation is division)
    then (check whether denominator is zero)
    else if (denominator is zero)
    then (show "maths error")
    else if (denominator is not zero)
    then (go to the calculating and round off value into 2 decimal places)
    if end
    printf (show answer)
    The above isn't pseudo code for your program. Just paraphrase your actual program, line by line.

    Don't rearrange the order of the logic.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    Code:
    print out Enter Calculation
    scan the maths expression
    if operation is addition
    then print out number 1+number 2=answer
    else if operation is substraction 
    then print out number 1-number 2=answer
    else if operation is multiplication
    then print out number 1*number 2=answer
    else if number 2 is zero
    then print maths error
    else if operation is division
    then print out number 1/number 2=answer



    How to write pseudo code for "#include <stdio.h>, int main (), float, char operation,while (1) and return 0?

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by yeohwl91 View Post
    How to write pseudo code for "#include <stdio.h>, int main (), float, char operation,while (1) and return 0?
    This is getting old. C Made Easy Tutorials. Start reading.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Calculator By C
    By husam_elmutasim in forum C Programming
    Replies: 2
    Last Post: 06-28-2011, 05:58 PM
  2. very simple calculator
    By N_D_E in forum C Programming
    Replies: 4
    Last Post: 03-22-2010, 07:18 PM
  3. simple calculator
    By HunterCS in forum C++ Programming
    Replies: 10
    Last Post: 07-18-2007, 06:51 AM
  4. Simple Calculator
    By Achillles in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 04:50 PM
  5. Having some problem with my simple calculator
    By Aven in forum C Programming
    Replies: 3
    Last Post: 07-21-2002, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21