Thread: Error in calculator program

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    Error in calculator program

    Hello.

    I am new to C++ programming and im trying to write rhis little calculator program and im getting some errors.

    If anyone could help me that would be greatly appreciated!

    Here is te code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int number1 = 0;
        int number2 = 0;
        float number3 = 0;
        int opcheck = 0;
        string operator1;
        
        cout << "Enter a whole number: ";
        cin >> number1;
        cout << "Enter another whole number: ";
        cin >> number2;
        do {
            cout << "what type of math would you like done?\nEnter the word in lower case plaese: ";
            cin >> operator1;
            
            if ( operator1 == "addition" ) { 
             number1 + number2 = number3;         //ERROR
             opcheck = 1; }
        else if ( operator1 == "subtraction" ) {
              number1 - number2 = number3;          //ERROR
              opcheck = 1; }
        else if ( operator1 == "division" ) {
              number1 / number2 = number3;          //ERROR
              opcheck = 1; }
        else if { operator1 == "multiplication" ) {           //ERROR
              number1 * number2 = number3;
              opcheck = 1; )
        else {
             cout << "Sorry, I didn't quite understand what you ment...\nRemember, 'add' is not an acceptible responce, but 'addition' is!" << endl;
            
            } 
        while ( opcheck == 0 );
        
        system("pause");
    }          
    //and for some reason im getting an error on this line but theres nothin on it.
    Im getting errors on the lines with the "ERROR" commented in.

    Thank you for the help!!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you expect those lines to do? Hint: = is not symmetric; what's on the left side and what's on the right side matters.

    You also need to count open-curly braces and close-curly braces (hint: you have eight open and five close).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    ......

    i dont know, please help me im so new at this.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    The left hand side of '=' should refer to the variable in which you would like the resulting expression (on the right hand side) to be stored. At the moment you are saying store number3 in number1 + number2
    Last edited by rocketman50; 04-05-2010 at 07:21 PM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Thank you, i finally got it working

    Thanks again

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Use switch statement in these type of situations. It's better. int main() means that it is returning some value after it's execution. Either make it void main() as in here or return any integer at the end usually zero ie, return 0;. It will justify your use of int main().

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by saqib_
    Use switch statement in these type of situations. It's better.
    Unfortunately, a switch would not be appropriate since one is comparing strings. (Unless you say, compute a perfect hash of the strings.)

    Quote Originally Posted by saqib_
    int main() means that it is returning some value after it's execution. Either make it void main() as in here or return any integer at the end usually zero ie, return 0;. It will justify your use of int main().
    void main() is non-standard. If control reaches the end of the global main function without encountering a return statement, the effect will be as if return 0; was present, so it already justifies "your use of int main()".
    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

Popular pages Recent additions subscribe to a feed