Thread: Basic calculator program in C++

  1. #31
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Hmmmmm someone who has used bloodshed should take over from here lol... I'm not familiar with it in the least. It sounds compiler specific... maybe try making prototypes and then placing all the definitions at the bottom of the code?

    Like this:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    void fu();
    void bar();
    int main()
    {
       //do stuff
       return 0;
    }
    void fu()
    {
       //do fu's stuff
    }
    void bar()
    {
       //do bar's stuff
    }

  2. #32
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Now it tells me "void '<insert one of the four math functions>()' used prior to declaration". So that didn't work.

  3. #33
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Lets see your code as it is now....

  4. #34
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Who knew writing a calculator program could be so tough? :P

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void plus();
    void minus();
    void multi();
    void divi();
    
    void calc() {
        string cal;
        
        cout << "Do you want to add, subtract, multiply, or divide?\n";
        cin.ignore(80,'\n');
        cin >> cal;
        if (cal == "add" || "Add") {
             plus();
        }
        else if (cal == "subtract" || "subtract") {
             minus();
        }
        else if (cal == "multiply" || "Multiply") {
             multi();
        }
        else if (cal == "divide" || "Divide") {
             divi();
        }
    }
    
    void again() {
         string d;
         
         cin >> d;
         if (d == "Y" || "y") {
                calc();
         }
         else {
         }
    }
    
    int main() 
    {
        string a;
    
        cout << "Do you wish to use the Calculator? Y/N: ";
        cin >> a;
        if (a == "Y" || "y") 
    	{
              calc();
              cout << "Would you like to use the calculator again? Y/N: ";
              cin >> a;
              while (a == "Y" || "y") 
    		  {
                    again();
                    cout << "Would you like to use the calculator again? Y/N: ";
                    cin >> a;
    		  }
    	}
        else 
    	{
             cin.ignore(80,'\n');
        }
        return 0;
    }
    
    void plus() {
           float a;
           float b;
           float c;
           
           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 minus() {
           float a;
           float b;
           float c;
           
           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 multi() {
           float a;
           float b;
           float c;
           
           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();
    }
    
    void divi() {
           float a;
           float b;
           float c;
           
           cout << "Input a number: ";
           cin >> a;
           cout << "Input a second number: ";
           cin >> b;
           c = a / b;
           cout << a << " divided by " << b << " equals " << c << ".";
           cin.ignore(80,'\n');
           cin.get();
    }

  5. #35
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    calc and again need to have prototypes too... not just a couple functions.

  6. #36
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Still the same errors.

  7. #37
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    I dont know what to tell you... my code I posted worked on MSVC++ ... and ran just fine except for having to enter y twice in a couple places...

  8. #38
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I noticed that bug earlier too. =\

    EDIT: Fixed that, it had to do with calling cin twice accedentally.
    Last edited by linkofazeroth; 08-27-2005 at 11:01 AM.

  9. #39
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Ok, I had the bright idea of assimilating plus() and minus() into multi() and divi(). That is, I now have two functions instead of four: multiplus() and diviminus(). Works just fine now. (will edit later if and when I run up another wall)
    Last edited by linkofazeroth; 08-27-2005 at 05:51 PM.

  10. #40
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Ok, I found the next wall. Now, if I input any of my options (add, subtract, multiply, divide), it goes off on this weird loop. First it runs the multiplication part of multiplus(), then the division part of diviminus(), then multiplies again, then divides again, and at the output of the division it prints "I'm sorry, please try again" and "Do you want to add, subtract, multiply, or divide?" all on the same line, looping the entire bug. Also, whether I input Y or N at the very start, it runs the program.


    Say nothing about the layout of the code, or what I could do better. I just want to get the bugs fixed first.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void plus();
    void minus();
    void multi();
    void divi();
    void calc();
    
    int main() 
    {
        string a;
    
        cout << "Do you wish to use the Calculator? Y/N: ";
        cin >> a;
        if (a == "Y" || "Y") {
              while (a == "Y" || "y") 
              {
                    calc();
                    cout << "Would you like to use the calculator again? Y/N: ";
                    cin >> a;
              }
        }
        else if (a == "N" || "n") {
             cin.ignore(80,'\n');
             return 0;
        }
    }
    
    void multiplus(string cal) {
           float a;
           float b;
           float c;
           
           if (cal == "multiply" || "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" || "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();
           }
           else {
           }
    }
    
    void diviminus(string cal) {
           float a;
           float b;
           float c;
           
           if (cal == "divide" || "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" || "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();
           }
           else {
           }
    }
    
    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" || "Add") {
                cas = 1;
        }
        else if (cal == "subtract" || "Subtract") {
             cas = 2;
        }
        else if (cal == "multiply" || "Multiply") {
             cas = 3;
        }
        else if (cal == "divide" || "Divide") {
             cas = 4;
        }
        else {
        }
        switch (cas) {
               case 1:
                    multiplus(cal);
               case 2:
                    diviminus(cal);
               case 3:
                    multiplus(cal);
               case 4:
                    diviminus(cal);
               default:
                       cout << "I'm sorry, please try again.";
                       calc();
        }
    }

  11. #41
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    I see one problem you didn't prototype your new functions.... In fact all the old prototypes are still there even though the functions aren't. I'll toss that code in my compiler and see what needs to be fixed.

  12. #42
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    In you main you are using
    Code:
    if (a == "Y" || "Y")
    this is always true because "Y" is a non zero number when converted to an int which when converted to bool is true( x != 0 is always true) and I believe you wanted "y" there and not "Y".

    In fact I think that is your main problem you forgot to put a == or cal == in most if not all of your if statements. That should help you some. When all else fails step through your program with the debugger it is useful.

  13. #43
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    DOH!! :P I completely forgot! That's definitly a downside of prototypes... And I fixed an error in my main() function's "if" arguments, it had "Y" || "Y" instead of "Y" || "y". Wandering through this mess, fixing some minor bugs...

    EDIT: Still the same major bug, though.

  14. #44
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    You need to make it
    Code:
    if(a == "Y" || a == "y")

  15. #45
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I don't really know how to use Dev-C++'s debugger... Otherwise I'll fix those if statements. Oops.

    The "Y" || "y" worked fine when I had only the plus() function.

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, 04: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