Basic calculator program in C++

This is a discussion on Basic calculator program in C++ within the C++ Programming forums, part of the General Programming Boards category; Here is what I came up with this might help yea some. Code: #include <iostream> #include <string> using namespace std; ...

  1. #46
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Here is what I came up with this might help yea some.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    void diviminus(string );
    void multiplus(string );
    void calc();
    
    int main() 
    {
        string a;
    
        cout << "Do you wish to use the Calculator? Y/N: ";
        cin >> a;
        if (a == "Y" || a == "y") 
    	{
              while (a == "Y" ||a == "y") 
              {
                    calc();
                    cout << "Would you like to use the calculator again? Y/N: ";
                    cin >> a;
              }
        }
        else 
    	{
             cin.ignore(80,'\n');
             return 0;
        }
    }
    
    void multiplus(string cal) 
    {
           float a;
           float b;
           float c;
           
           if (cal == "multiply" || cal == "Multiply") 
    	   {
              cout << "Input a number: ";
              cin >> a;
              cout << "Input a second number: ";
              cin >> b;
              c = a * b;
              cout << a << " times " << b << " equals " << c << ".";
              cin.ignore(80,'\n');
              cin.get();
           }
           else if (cal == "add" || cal == "Add") 
    	   {
              cout << "Input a number: ";
              cin >> a;
              cout << "Input a second number: ";
              cin >> b;
              c = a + b;
              cout << a << " plus " << b << " equals " << c << ".";
              cin.ignore(80,'\n');
              cin.get();
           }
    }
    
    void diviminus(string cal) {
           float a;
           float b;
           float c;
           
           if (cal == "divide" || cal == "Divide") {
                   cout << "Input a number: ";
                   cin >> a;
                   cout << "Input a second number: ";
                   cin >> b;
                   c = a / b;
                   cout << a << " divided by " << b << " equals " << c << ".";
                   cin.get();
           }
           else if (cal == "subtract" || cal == "Subtract") {
                cout << "Input the number you want to subtract from: ";
                cin >> a;
                cout << "Input the amout you with to subtract: ";
                cin >> b;
                c = a - b;
                cout << a << " minus " << b << " equals " << c << ".";
                cin.ignore(80,'\n');
                cin.get();
           }
    }
    
    void calc() 
    {
        string cal;
        int cas;
        
        cout << "Do you want to add, subtract, multiply, or divide?\n";
        cin.ignore(80,'\n');
        cin >> cal;
        if (cal == "add" || cal ==  "Add") 
    	{
             multiplus(cal);
        }
        else if (cal == "subtract" || cal ==  "Subtract") 
    	{
             diviminus(cal);
        }
        else if (cal == "multiply" || cal ==  "Multiply") 
    	{
             multiplus(cal);
        }
        else if (cal == "divide" || cal == "Divide") 
    	{
             diviminus(cal);
        }
    	else
    	{
              cout << "I'm sorry, please try again.";
              calc();
        }
    }

  2. #47
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Thanks, I'll compare that code to mine in a sec. Turns out you're right about needing cal == "Y" || cal == "Y", as it fixed the "only goes to multi" problem. It still has that wonky bug, exept this time it just repeats the selected function twice, then displays "I'm sorry, please try again" and "Do you want to add, subtract, multiply, or divide?"

  3. #48
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Code works perfectly now, thanks! Now how would you suggest I make it so you can add as many numbers as you want into the calculations? Like you could input

    4 5 7 2 10

    and it would output the sum of 4 plus 5 plus 7 plus 2 plus 10, and yet could input

    8 1 3 10 2 6 8

    without messing up the innards of the code?
    Last edited by linkofazeroth; 08-27-2005 at 07:38 PM.

  4. #49
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Well if you were going to do that would all the numbers be input at the same time or would your output looks somethng like:
    Code:
    Do you want to add, subtract, multiply, or divide?
    add
    Input a number: 5
    Input a number: 6
    5 plus 6 equals 11
    do you want to add another number? Y
    Input a number: 7
    11 plus 7 equals 18
    do you want to add another number? N
    Or what kind of formating do you want?

  5. #50
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Preferrably your code-box'ed example, since it's more user friendly (or so I personally think).

  6. #51
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    It wouldn't be much of a code change... you would use recursive calls to do that.... do you need an example?

  7. #52
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I'm not too used to using recursive calls, but now that I think of it, I could just use 3 floats and a while loop. :P

  8. #53
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Yea you can use a while loop :P was thinking a bit differently and in this case a while would be more efficent I think... Here is an example for a recursive call in case you want to play with it some:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void recursivefunction(int );
    
    int main()
    {
       recursivefunction(x);
    }
    
    
    void recursivefunction(int x)
    {
       int y;
       string a;
       cout<<"Enter a number: ";
       cin>>y;
       cout<<x<<" plus "<<y<<" equals "<<x+y;
       cout<<"Do you want to add another number? ";
       cin>>a;
       if(a == "y" || a == "Y")
       {
          recursivefunction(x+y);
       }
    }

  9. #54
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Ahh, I see. But don't recursives have a limit on how many times they can recurse? I don't know how many, since I didn't test the recurse example in the recursion part of the tutorial here, though.

  10. #55
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I just added this block of code onto the end of each half of the two math functions, and it works!

    Code:
                cout << "Would you like to add in another number? Y/N\n";
                cin >> yn;
                while (yn == "Y" || yn == "y") {
                      cout << "Input a number: ";
                      cin >> b;
                      c += b;
                      cout << "Your new total is " << c << ".\n";
                      cout << "Would you like to add in another number? Y/N\n";
                      cin >> yn;
                }

  11. #56
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I'm trying to add support for modulus into the calculator, but what exactly is the operator for it? I'm using the statement c = a % b; for the main part of the function, and c %= b; for the while loop.

  12. #57
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    The limit of a recursive function is the amount of RAM in the computer since each call takes a little bit more data... modulus is used with the syntax
    Code:
    a = b%c;
    it will give you the remander if b was divided by c.

    It is also used in generating random numbers as an example:

    Code:
    .
    .
    .
    int randomnumber;
    srand(clock());
    randomnumber = 1+rand()%100;     //this is a number between 1 and 100
    randomnumber = 25+rand()%76;     //this is a number between 25 and 100
    .
    .
    .

  13. #58
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    On the uses of modulus, neat. On the syntax of it, you mean I would use it as "c = a%b" and "c %=b" instead? Or am I wrong?

  14. #59
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Code:
    c %= b;
    this is equivalent to the statement:
    If it even compiles I have never used it like that...
    Code:
    c = c%b;

  15. #60
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Well, it doesn't compile, hehe. Even the x = x%x version doesn't compile, though. It tells me "invalid operands of types "float" and "float" to binary "operator%"". Is there any special header file I need?

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 05:28 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  4. Basic Window Creation, Dev C++ 4.9.9.0 Linking Error
    By Tronic in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2004, 06:03 PM

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