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; Aaaaaah, I see. Maybe I should implement a pointer into my calculator... not that it would affect the output much, ...

  1. #16
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Aaaaaah, I see. Maybe I should implement a pointer into my calculator... not that it would affect the output much, but since I'm a newbie at C++, it's good to experiment.

  2. #17
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    There is no real need to add a pointer in this context. In particular, it is not a particularly good design choice, in my opinion. There is no reason not to simply return the value.

    Going back to your original question for a moment, before you start playing around with pointers:

    However, when dealing with parameters, remember that they are copies of the data you send to them, and hence, the variables themselves are local to the function. Even pointers and references are copies (understanding this is an important point).

    For example:
    Code:
    #include <iostream>
    
    void foo(int* x)
    {
       int a;
       x = &a;
       *x = 5;
    }
    
    void bar(int* x)
    {
       *x = 5;
    }
    
    int main()
    {
       int y = 1;
       std::cout << y <<std::endl;
       foo(&y);
       std::cout << y <<std::endl;
       bar(&y);
       std::cout << y <<std::endl;
    }
    Notice that when you change the value of x, you are changing what the temporary (pointer) variable is assigned to (changing what address it points at), so there is no effect on the passed parameter. However, assuming you leave x pointing to the same address passed to the function, it will change your variable, but you are passing an address-of-an-int (which is a value), not an int.

    References work the same way in principle, but they have special syntax to make them a bit more transparent. You cannot reassign what a reference points to, and after its initializations, assigning anything to it is really making an assignment to the space in memory that it points to (as I said, special syntax). A consequence of this is, if it would ever be valid to pass a NULL argument, use a pointer... always. In general, it is good practice to prefer passing references when possible as it will make shooting yourself in the foot harder.

    As a little example, if you wanted to reassign a pointer (e.g. what foo looks like it does, but doesn't), then you can pass (for example) a reference to a pointer (you could, of course pass a pointer to a pointer... though follow the same guidelines outlined above):
    Code:
    #include <iostream>
    
    void foo(int*& ptr)
    {
       ptr = new int;
       *ptr = 7;
    }
    
    int main()
    {
       int* p = 0;
       foo(p);
       if(!foo)
          std::cout << "Hmm... something is amiss here..." << std::endl;
       else
          std::cout << "*p = " << *p << std::endl;
    }
    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #18
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Ok, I'm kind of stumped again as to what the compiler's complaining about. Here's the fuction with the problem, the compiler errors are "'plus' undeclared (first use this function)" and "'minus' undeclared (first use this function)".

    Code:
    int calc() {
        string cal;
        
        cout << "Do you want to add, subtract, multiply, or divide?\n";
        cin >> cal;
        if (cal == "add") {
                plus();
        }
        else if (cal == "Add") {
             plus();
        }
        else if (cal == "subtract") {
             minus();
        }
        else if (cal == "Subtract") {
             minus();
        }
        else if (cal == "multiply") {
             multi();
        }
        else if (cal == "Multiply") {
             multi();
        }
        else if (cal == "divide") {
             divi();
        }
        else if (cal == "Divide") {
             divi();
        }
    }

  4. #19
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Make sure plus and minus are defined before this function.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #20
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Plus() and minus() are the first two defined. Here, I'll post my whole code...

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

  6. #21
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I don't immediately see why you are having trouble with plus() and minus(), but I do notice that although you say that those first four functions return an int, you have no return statements in them, and the return values (if there were any) are never used. They should have a 'void' return type.

    Make sure plus and minus are defined before this function.
    They needn't be defined before, just declared (although a definition is more than sufficient).
    Code:
    void foo( ); // <--- declaration
    
    void foo( )
    {
    } // <--- definition
    Last edited by Zach L.; 08-26-2005 at 07:40 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #22
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Well, the error seems to be in the calc() function, from what the compiler is telling me. Something about the lines "plus();" and "minus();" are bugging it, but what's odd is that multi() and divi() are fine. I copied and pasted all four from the original plus(), modifying it to fit what they're supposed to do, but you'd think that if I copied and pasted all of them, they'd all have the problem...

  8. #23
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    Code:
    #include <iostream>
    
    using namespace std;
    
    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();
    }
    
    void calc() {
        string cal;
        
        cout << "Do you want to add, subtract, multiply, or divide?\n";
        cin.ignore(80,'\n');
        cin >> cal;
        if (cal == "add" || cal == "Add") {
             plus();
        }
        else if (cal == "subtract" || cal == "subtract") {
             minus();
        }
        else if (cal == "multiply" || cal == "Multiply") {
             multi();
        }
        else if (cal == "divide" || "Divide") {
             divi();
        }
    }
    
    void again() {
         string d;
         
         cin >> d;
         if (d == "Y" || d == "y") {
                calc();
         }
         else {
         }
    }
    
    int main() {
        string a;
    
        cout << "Do you wish to use the Calculator? Y/N: ";
        cin >> a;
        if (a == "Y" || a == "y") {
              calc();
              cout << "Would you like to use the calculator again? Y/N: ";
              cin >> a;
              while (a == "Y" || a == "y") {
                    again();
                    cout << "Would you like to use the calculator again? Y/N: ";
                    cin >> a;
              }
        else {
             cin.ignore(80,'\n');
        }
        return 0;
    }
    Usually compilers just give warnings on no returns maybe you have an option set that makes all warnings errors... try this code. I have also added 80,'\n' to the cin.ignore()'s. That will allow you to enter up to 80 chars or untill you press enter.

  9. #24
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    No, that still gives the same errors... and warnings are still warnings by my settinngs.

  10. #25
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    Broke out the compiler... this compiles for me. You had an if{ with no '}' and a couple other errors.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    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();
    }
    
    void calc() {
        string cal;
        
        cout << "Do you want to add, subtract, multiply, or divide?\n";
        cin.ignore(80,'\n');
        cin >> cal;
        if (cal == "add" || cal == "Add") {
             plus();
        }
        else if (cal == "subtract" || cal == "subtract") {
             minus();
        }
        else if (cal == "multiply" || cal == "Multiply") {
             multi();
        }
        else if (cal == "divide" || "Divide") {
             divi();
        }
    }
    
    void again() {
         string d;
         
         cin >> d;
         if (d == "Y" || d == "y") {
                calc();
         }
         else {
         }
    }
    
    int main() 
    {
        string a;
    
        cout << "Do you wish to use the Calculator? Y/N: ";
        cin >> a;
        if (a == "Y" || a == "y") 
    	{
              calc();
              cout << "Would you like to use the calculator again? Y/N: ";
              cin >> a;
              while (a == "Y" || a == "y") 
    		  {
                    again();
                    cout << "Would you like to use the calculator again? Y/N: ";
                    cin >> a;
    		  }
    	}
        else 
    	{
             cin.ignore(80,'\n');
        }
        return 0;
    }

  11. #26
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    What's the <string> header file do?

  12. #27
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    Lets you use string.... which is what you are doing... it does alot more than that too lets you compare etc add.

  13. #28
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I thought you were able to use the string type without that header file? And I still have the same exact errors.

  14. #29
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    I got no errors.... and I needed to include it to get rid of the errors about the comparisons and such... no idea why it won't work for you I am using Microsoft's compiler 7.1 maybe that has something to do with it post your errors?

  15. #30
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I'm using Bloodshed Dev-C++ as an IDE / compiler-linker. The errors, exactly as the compile shows them, are...

    In function `void calc()':
    `plus' undeclared (first use this function)
    `minus' undeclared (first use this function)

    I'm stumped.

Page 2 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, 09: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, 07: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, 05:03 PM

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