Thread: calculator

  1. #1
    Registered User MOH123's Avatar
    Join Date
    Aug 2005
    Posts
    20

    calculator

    well this is my first actual project with C++. Im trying to make a basic calculator, +, -, *, and /. I first had a problem on hwo to loop it so it wont shut down after only doing 1 problem. i have that figured out but now i need to know one thing, How can i make all of them into the same code? uhh that doesnt make sense. I mean like if they type 1+1 it will show 2, then restart and if they type 2*2 or whatever it will show the answer. then whenever they type a problem it will restart. any body get this? any further questions i have i will also post here.

    -thanks in advance
    Last edited by MOH123; 09-14-2005 at 04:06 PM.
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You mean parse the input so when 1 + 1 is typed it will read that and output 2. You would have to read this into a string and then tokenize the string read the numbers before the operand and read the operand and then read the numbers after the operand. and then do the math
    Woop?

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Maybe you need a while loop, and a break statement if the user enters "quit" or something?

    Post your code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Speaking of parsing strings and making a basic calculator, you might enjoy this FAQ entry on tokenizing user input to evaluate it as a basic expression.

    FAQ > How do I... (Level 3) > Separate a string into tokens? (C++)

    Feel free to post any and all code you have. As mentioned by dwks, you could throw your entire main() function in a while loop so that it repeatedly executes; returns to the top of main, every time it finishes.

  5. #5
    Registered User MOH123's Avatar
    Join Date
    Aug 2005
    Posts
    20
    ok, i found how to do the while code and it works for adding and subtracting but when i try to do it for * or / it will just repeat equals 2 forevor. here is code /*i know it is very messy and i probably have stuff in there i dont need but remember this is my first project so please go easy on me.*/

    Code:
    #include <iostream>
    
    using namespace std;
    
    int add (int a, int b);
    int sub (int c, int d);
    int mult (int e, int f);
    int divi (int g, int h);
    
    int main()
    {
        int repeat;
        int a;
        int b;
        int c;
        int d;
        int select;
        
        repeat = 0;
        do {
        cout<<"enter two numbers: ";
        cin>> a >> b;
        cout<<"="<<add (a,b) <<"\n";
        cout<<"\n";
    } while ( repeat != 1);
    {
            int c;
            int d;
            
            repeat = 0;
            cout<<"enter two numbers: ";
            cin>> c >> d;
            cout<<"="<<sub (c,d) <<"\n";
            cout<<"\n";
    } while ( repeat != 1);
    {
            int e;
            int f;
            
            repeat = 0;
            cout<<"enter two numbers: ";
            cin>> e >> f;
            cout<<"= "<<mult (e,f) <<"\n";
            cout<<"\n";
    } while ( repeat != 1);
    {
            int g;
            int h;
            
            repeat = 0;
            cout<<"enter two numbers: ";
            cout<<"= "<<divi (g,h) <<"\n";
            cout<<"\n";
    } while ( repeat != 1);
    cin.get();
    }
    
    int add (int a,int b)
    {
        return a + b;
    }
    int sub (int c, int d)
    {
        return c - d;
    }
    int mult (int e, int f)
    {
        return e * f;
    }
    int divi (int g, int h)
    {
        return g / h;
    }
    if there are things i dont need please tell, and if you could help me.

    -thanks
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well,
    Code:
    repeat = 0;
        do {
        cout<<"enter two numbers: ";
        cin>> a >> b;
        cout<<"="<<add (a,b) <<"\n";
        cout<<"\n";
    } while ( repeat != 1);
    That's the same as
    Code:
        {
            cout<<"enter two numbers: ";
            cin>> a >> b;
            cout<<"="<<add (a,b) <<"\n";
            cout<<"\n";
        }
    Or, for that matter, no braces at all. But what you need is something like this:

    Code:
    int main() {
        int one, two;
        char op;
    
        while(1) {
            cout << "Enter the operator: ";
            cin >> op;
            if(op == 'q') break;
    
            // get two numbers and process them . . . .
        }
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User MOH123's Avatar
    Join Date
    Aug 2005
    Posts
    20
    hmm i dont get that, nothing happens when i use that code. could you explain better? like where to i put it and stuff?

    -thanks
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

  8. #8
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by MOH123
    hmm i dont get that, nothing happens when i use that code. could you explain better? like where to i put it and stuff?

    -thanks
    Take his code, and add your code (to get 2 values and add them, and the add function) to where he had the commented line (your code is the red part):

    Code:
    #include <iostream>
    
    using namespace std;
    
    int add (int a, int b);
    int sub (int c, int d);
    int mult (int e, int f);
    int divi (int g, int h);
    
    int main() {
        int one, two;
        char op;
        int repeat;
        int a;
        int b;
        int c;
        int d;
        int select;
    
        while(1) {
            cout << "Enter the operator: ";
            cin >> op;
            if(op == 'q') break;
    
        cout<<"enter two numbers: ";
        cin>> a >> b;
        cout<<"="<<add (a,b) <<"\n";
        cout<<"\n";
    
    cin.get();
    
        }
    
        return 0;
    }
    
    int add (int a,int b)
    {
        return a + b;
    }
    
    int sub (int c, int d)
    {
        return c - d;
    }
    
    int mult (int e, int f)
    {
        return e * f;
    }
    
    int divi (int g, int h)
    {
        return g / h;
    }
    Although doing this now only lets you use the add function, and not the subtract/multiple/divide functions. So I would add a switch statement, and if the cin variable was == d, it would go to case 'd' and divide the 2 numbers, same thing goes for m, s, and a.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int add (int a, int b);
    int sub (int c, int d);
    int mult (int e, int f);
    int divi (int g, int h);
    
    int main() {
        char op;
        int a;
        int b;
    
    while (1) {
            cout << "Enter the operator: ";
            cin >> op;
    
            if(op == 'q')
              break;
    
        cout<<"enter two numbers: ";
        cin>> a >> b;
    
            switch(op) {
              case 'a':
                cout<<"="<<add (a,b) <<"\n" << endl;
              continue;
              case 's':
                cout<<"="<<sub (a,b) <<"\n" << endl;
              continue;
              case 'm':
                cout<<"="<<mult (a,b) <<"\n" << endl;
              continue;
              case 'd':
                cout<<"="<<divi (a,b) <<"\n" << endl;
              continue;
              default:
                cout << "Nothing was entered\n" << endl;
              continue;
           } //end the switch statement
    
      break;
    } //end the while statement
    
    cin.get();
    
        return 0;
    }
    
    int add (int a,int b)
    {
        return a + b;
    }
    
    int sub (int c, int d)
    {
        return c - d;
    }
    
    int mult (int e, int f)
    {
        return e * f;
    }
    
    int divi (int g, int h)
    {
        return g / h;
    }
    So this basicly says it will keep asking the user the operator and the numbers to multiply, until when asked for the operator the user enters: q. Entering a will do the add function, m will do multiply, d will do division, and s will do subtraction. Entering nothing will go to default. The continue keyword is used to go back up to the top of the loop (top of the while statement). So after finishing the addition/etc it will go back up to the top of the while(1) and ask for the operator again.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Registered User MOH123's Avatar
    Join Date
    Aug 2005
    Posts
    20
    ok thanks but just one thing, when entering the operator, i know i have to do a,d,s,or m, but if somebody enters something wrong by accident then it will just continuously loop "= Nothing was entered" is there a way to fix this? thanks again for helping.
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by MOH123
    ok thanks but just one thing, when entering the operator, i know i have to do a,d,s,or m, but if somebody enters something wrong by accident then it will just continuously loop "= Nothing was entered" is there a way to fix this? thanks again for helping.
    I'm not sure, but maybe put this if statement in the program (cin.fail() one):

    Code:
    while (1) {
            cout << "Enter the operator: ";
            cin >> op;
    
            if(cin.fail()) {
              op = 0;
              continue;
            }
    
            if(op == 'q')
              break;
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 12:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM