Thread: Calculator using if and else if statement

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    30

    Unhappy Calculator using if and else if statement

    Can anyone help me with this code. I am trying to make a calculator using if and else if statements. So far i can perform the functions of addition, subtraction , multiplication and division . But when it comes to modulus function i am getting errors.
    As modulus does not support float data type i am converting to integer before i perform modulus operation.
    Following are the errors:
    Error 7 error C2143: syntax error : missing ';' before 'type'
    Error 8 error C2143: syntax error : missing ';' before 'type'
    Error 9 error C2065: 'i' : undeclared identifier
    Error 10 error C2065: 'j' : undeclared identifier

    I have made all the necessary declarations but i still get these errors.

    This is part of the modulus function code:
    Code:
    else if (operation=='5')
        {
         printf("Enter the values:\n");
         scanf("%f%f", &val1,&val2);
         int i=val1;  //(error 7)
         int j=val2; //(error 8)
         remainder=i%j; //(error 9 and 10)
         printf("%f\n\n",remainder);
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Out of curiosity, but what compiler are you using? Are you compiling with respect to the 1999 edition of the C standard or later?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    I am using microsoft visual studio 2012.
    And I am not sure of the answer to your second question. I am fairly new at C programming.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's okay. You need to know that because you are using a Microsoft compiler, you're compiling with respect to an older edition of the C standard. In that version of C, variables must be declared at the start of a block, before other code. Thus, this:
    Code:
    else if (operation=='5')
    {
        printf("Enter the values:\n");
        scanf("%f%f", &val1,&val2);
        int i=val1;
        int j=val2;
        remainder=i%j; //(error 9 and 10)
        printf("%f\n\n",remainder);
    }
    should be:
    Code:
    else if (operation=='5')
    {
        int i;
        int j;
        printf("Enter the values:\n");
        scanf("%f%f", &val1, &val2);
        i = (int)val1;
        j = (int)val2;
        remainder = i % j;
        printf("%f\n\n", remainder);
    }
    Though it would be simpler to either read the values as integers to begin with, or use fmod from <math.h> if that makes sense, or cast in the modulo expression:
    Code:
    else if (operation=='5')
    {
        printf("Enter the values:\n");
        scanf("%f%f", &val1, &val2);
        remainder = (int)val1 % (int)val2;
        printf("%f\n\n", remainder);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Wow! thank you so much @laserlight.
    I tried all except the fmod(because i don't understand) and they work flawlessly.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post

    [...]

    should be:
    Code:
    else if (operation=='5')
    {
        int i;
        int j;
        printf("Enter the values:\n");
        scanf("%f%f", &val1, &val2);
        i = (int)val1;
        j = (int)val2;
        remainder = i % j;
        printf("%f\n\n", remainder);
    }
    Though it would be simpler to either read the values as integers to begin with, or use fmod from <math.h> if that makes sense, or cast in the modulo expression:
    Code:
    else if (operation=='5')
    {
        printf("Enter the values:\n");
        scanf("%f%f", &val1, &val2);
        remainder = (int)val1 % (int)val2;
        printf("%f\n\n", remainder);
    }
    How does that work? val1 and val2 are already (declared as) integers, so the error is actually in the scanf() conversion specifiers, not in the modulo operation.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hodor
    val1 and val2 are already (declared as) integers
    I see that you've been peeking over shan2014's shoulder to read the declaration of val1 and val2
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    I see that you've been peeking over shan2014's shoulder to read the declaration of val1 and val2
    Err... Hodor, hodor, hodor!

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by Hodor View Post
    How does that work? val1 and val2 are already (declared as) integers, so the error is actually in the scanf() conversion specifiers, not in the modulo operation.
    Just so that this air of confusion is settled, I have declared val1 and val2 as float data type (in the start of initialization) and that's why I got the error!!!

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Err... Hodor, hodor, hodor!
    (」゜ロ゜)」

    THAT. EXPLAINS. EVERYTHING!

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by phantomotap View Post
    (」゜ロ゜)」

    THAT. EXPLAINS. EVERYTHING!
    Of course it does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  2. Calculator
    By maxorator in forum Tech Board
    Replies: 3
    Last Post: 10-14-2005, 01:00 AM
  3. calculator
    By MOH123 in forum C++ Programming
    Replies: 9
    Last Post: 09-14-2005, 07:06 PM
  4. Calculator v3.0.0
    By Quantrizi in forum Game Programming
    Replies: 6
    Last Post: 10-25-2002, 05:44 AM
  5. Calculator v2.2.0
    By Quantrizi in forum Game Programming
    Replies: 7
    Last Post: 10-08-2002, 04:29 PM

Tags for this Thread