Thread: My calculator in Dividing seems wrong I think

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    19

    Question My calculator in Dividing seems wrong I think

    Code:
    #include <stdio.h>
    
    
    int main () {
        char a;
        printf("Enter 'M' for Multiplication 'D' for Division and 'A' for Addition and 'S' for Subtraction: ");
        scanf("%c", &a);
        int num1, num2;
        printf("Enter first integer: ");
        scanf("%d", &num1);
        printf("Enter second integer: ");
        scanf("%d", &num2);
        int prod, quo, sum, diff;
        switch (a) {
            case 'M':
                prod = num1*num2;
                printf("%d * %d = ", num1,num2);
                printf("%d", prod);
                break;
            case 'D':
                num2;
                if (num2 == 0)
                printf("Error can't divide\n");
                else
                quo = num1/num2;
                printf ("%d / %d = ", num1, num2);
                printf("%d", quo);
                break;
            case 'A':
                sum = num1+num2;
                printf("%d + %d = ", num1, num2);
                printf("%d", sum);
                break;
            case 'S':
                diff = num1-num2;
                printf("%d - %d = ", num1, num2);
                printf("%d", diff);
                break;
            default:
                printf("Error invalid all words that are synonym to the first two words of this sentence");
            break;
        }
    }
    When I try going to Divide and I enter my first integer any above 0 and I type 0 in the 2nd integer it will still print what it says in the 'if' statement and still prints what it does in 'else'. What I want to happen is when 'num2 = 0' it will just prints that it will not divide it will just be undefined and it will just terminate without printing '12 / 0 = 0' I mean that is just wrong. Why does it still go on printing?

    --New member here please correct me if I've done wrong in the forum rules thanks.--

    EDIT: Also in dividing I want to get the remainder of it if possible. How to do that?
    EDIT #2: OH found the remainder thingy. Just need to put an integer variable that is equal to 'num1%num2' (remainder = num1%num2) and put it in CASE D. But must be after the quo=num1/num2 and before the printing of the remainder value.

    EDIT #3: For some reason my calculator (program) just crashes when I type 0 in the 2nd integer. BUt there are times it wont crash.
    Last edited by YaZao; 07-27-2015 at 07:01 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by YaZao View Post
    When I try going to Divide and I enter my first integer any above 0 and I type 0 in the 2nd integer it will still print what it says in the 'if' statement and still prints what it does in 'else'. What I want to happen is when 'num2 = 0' it will just prints that it will not divide it will just be undefined and it will just terminate without printing '12 / 0 = 0' I mean that is just wrong. Why does it still go on printing?
    Well, let's look at your code for case 'D':

    Code:
    if (num2 == 0)
    printf("Error can't divide\n");
    else
    quo = num1/num2;
    printf ("%d / %d = ", num1, num2);
    printf("%d", quo);
    Now let's apply some indentation, for us to help see the flow of logic:

    Code:
    if (num2 == 0)
        printf("Error can't divide\n");
    else
        quo = num1/num2;
    
    printf ("%d / %d = ", num1, num2);
    printf("%d", quo);
    There's the problem. Only a single statement (or block) is executed after an "if" or "else".

    If you want a series of statements to be executed, they must be within a block:

    Code:
    if (num2 == 0)
    {
        printf("Error can't divide\n");
    }
    else
    {
        quo = num1/num2;
        printf ("%d / %d = ", num1, num2);
        printf("%d", quo);
    }


    Quote Originally Posted by YaZao View Post
    --New member here please correct me if I've done wrong in the forum rules thanks.--
    Welcome to the forum, then. You've done quite well for a first post ... a clear question, using code tags, and a decent (albeit incomplete) attempt at indentation.

    Quote Originally Posted by YaZao View Post
    EDIT: Also in dividing I want to get the remainder of it if possible. How to do that?
    EDIT #2: OH found the remainder thingy. Just need to put an integer variable that is equal to 'num1%num2' (remainder = num1%num2) and put it in CASE D. But must be after the quo=num1/num2 and before the printing of the remainder value.
    EDIT #3: For some reason my calculator (program) just crashes when I type 0 in the 2nd integer. BUt there are times it wont crash.
    Is this before or after the modifications you've implied (adding the modulus operator, or '%')?

    I assume it's after, as you might be using the modulus operator with the second number being a zero since the code after "else" is not isolated to that "else" as I've already explained.

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    19

    Thumbs up

    Quote Originally Posted by Matticus View Post
    Well, let's look at your code for case 'D':

    Code:
    if (num2 == 0)
    printf("Error can't divide\n");
    else
    quo = num1/num2;
    printf ("%d / %d = ", num1, num2);
    printf("%d", quo);
    Now let's apply some indentation, for us to help see the flow of logic:

    Code:
    if (num2 == 0)
        printf("Error can't divide\n");
    else
        quo = num1/num2;
    
    printf ("%d / %d = ", num1, num2);
    printf("%d", quo);
    There's the problem. Only a single statement (or block) is executed after an "if" or "else".

    If you want a series of statements to be executed, they must be within a block:

    Code:
    if (num2 == 0)
    {
        printf("Error can't divide\n");
    }
    else
    {
        quo = num1/num2;
        printf ("%d / %d = ", num1, num2);
        printf("%d", quo);
    }




    Welcome to the forum, then. You've done quite well for a first post ... a clear question, using code tags, and a decent (albeit incomplete) attempt at indentation.



    Is this before or after the modifications you've implied (adding the modulus operator, or '%')?

    I assume it's after, as you might be using the modulus operator with the second number being a zero since the code after "else" is not isolated to that "else" as I've already explained.
    Oh yeah Now I see. ) Thanks. But it still crashes damn. Can you recommend me a good compiler? I'm using Dev C++ since it is somewhat useful I can use C++ and C language in one program.

    Yeah I tried to use my best english to ask a question here. Haha, forgive my newbie indentions since I am new to programming and I try to indent it as clear as possible for me to read and if possible to other readers.

    Also the last question what do you mean? XD Are you talking about the remainder? My code was like this after the block you showed me and I learned from you.

    Code:
            num2;
                if (num2 == 0) {
                    printf("Error can't divide\n");
                }
                else {
                    quo = num1/num2;
                    remainder=num1%num2;
                    printf ("%d / %d = ", num1, num2);
                    printf("%d", quo);
                    printf("\nRemainder: %d", remainder);
                }    
                break;
    Almost forgot. I declared remainder as an integer equal to num1%num2 first. (int remainder=num1%num2)
    Last edited by YaZao; 07-27-2015 at 07:57 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by YaZao View Post
    Oh yeah Now I see. ) Thanks. But it still crashes damn.
    Post your updated code if you need further help.

    Quote Originally Posted by YaZao View Post
    I'm using Dev C++ since it is somewhat useful I can use C++ and C language in one program.
    Dev C++ is an IDE, not a compiler. It is also quite old (unless you're using the Orwell version). There are other good IDEs to consider, such as Code Blocks.

    Quote Originally Posted by YaZao View Post
    Yeah I tried to use my best english to ask a question here. Haha, forgive my newbie indentions since I am new to programming and I try to indent it as clear as possible for me to read and if possible to other readers.
    Your english seems fine. Your indentation is pretty good, but you want to indent a new level when your code is "under" a loop or control statement. See here: https://en.wikipedia.org/wiki/Indent_style

    Quote Originally Posted by YaZao View Post
    Also the last question what do you mean? XD Are you talking about the remainder? My code was like this after the block you showed me and I learned from you.

    ...

    Almost forgot. I declared remainder as an integer equal to num1%num2 first. (int remainder=num1%num2)
    As I said, post your complete updated code if you want further assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple calculator: what's wrong with this code?
    By zer0_r00t in forum C Programming
    Replies: 14
    Last Post: 06-01-2014, 09:13 PM
  2. Help! Calculator Not Working...What is wrong?
    By usernamed in forum C++ Programming
    Replies: 7
    Last Post: 08-08-2013, 10:52 PM
  3. What's wrong with the code(Calculator program)
    By rizwan in forum C Programming
    Replies: 10
    Last Post: 10-19-2011, 05:59 PM
  4. What's wrong with my calculator?
    By andrewmin in forum C Programming
    Replies: 3
    Last Post: 09-08-2010, 02:30 PM
  5. Replies: 3
    Last Post: 03-16-2010, 02:29 PM

Tags for this Thread