Calculator program problemo

This is a discussion on Calculator program problemo within the C++ Programming forums, part of the General Programming Boards category; Code: #include <cstdio> #include <cstdlib> #include <string> #include <cmath> #include <iostream> using namespace std; float Squareroot(float x); float Add(float x, ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    Calculator program problemo

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    float Squareroot(float x);
    float Add(float x, float y);
    float Subtract(float x, float y);
    float Multiply(float x, float y);
    float Divide(float x, float y);
    
    int main() {
        
        cout << "Math Program v.1.8" << endl;
        
        float numberA;
        float numberB;
        cout << "What two numbers would you like to input?" << endl;
        cin >> numberA;
        cin >> numberB;
       
        cout << "Would you like to: \n" ;
        cout << "Square[r]oot?" << endl;
        cout << "[a]dd" << endl;
        cout << "[s]ubtract" << endl;
        cout << "[m]ultiply" << endl;
        cout << "or [d]ivide the numbers?" << endl;
        
        float funcvarbname; 
        
        string Option;
        if (Option == "r") {funcvarbname=Squareroot(numberA);}; 
        if (Option == "a") {funcvarbname=Add(numberA, numberB);};
        if (Option == "s") {funcvarbname=Subtract(numberA, numberB);};
        if (Option == "m") {funcvarbname=Multiply(numberA, numberB);};
        if (Option == "d") {funcvarbname=Divide(numberA, numberB);};
        
        cout << funcvarbname << endl;
        
        system("PAUSE");
        return 0;
    }
    Well I fixed my original error, but now it doesn't print the answers to my functions! Help please!
    Last edited by blankstare77; 09-04-2005 at 10:05 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,615
    it doesn't print the answers to my functions!
    Um, isnt that because you arent printing anything with say, cout?
    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

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    I was using cout. But anyway I fixed it:

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    float Squareroot(float x);
    float Add(float x, float y);
    float Subtract(float x, float y);
    float Multiply(float x, float y);
    float Divide(float x, float y);
    
    int main() {
        
        cout << "Math Program v.1.8" << endl;
        
        float numberA;
        float numberB;
        cout << "What two numbers would you like to input?" << endl;
        cin >> numberA;
        cin >> numberB;
       
        cout << "Would you like to: \n" ;
        cout << "Square[r]oot?" << endl;
        cout << "[a]dd" << endl;
        cout << "[s]ubtract" << endl;
        cout << "[m]ultiply" << endl;
        cout << "or [d]ivide the numbers?" << endl;
        
        float funcvarbname; 
        
        string Option;
        cin >> Option;
        
        if (Option == "r") {funcvarbname=Squareroot(numberA);}; 
        if (Option == "a") {funcvarbname=Add(numberA, numberB);};
        if (Option == "s") {funcvarbname=Subtract(numberA, numberB);};
        if (Option == "m") {funcvarbname=Multiply(numberA, numberB);};
        if (Option == "d") {funcvarbname=Divide(numberA, numberB);};
        
        cout << "YOUR ANSWER IS: " << funcvarbname << endl;
        
        system("PAUSE");
        return 0;
    }
    
    float Squareroot(float x) {
          return sqrt(x);
          }
    float Add(float x, float y) {
          return x+y;
          }
    float Subtract(float x, float y) {
          return x - y;
          }
    float Multiply(float x, float y) {
          return x*y;
          }
    float Divide(float x, float y) {
          return x / y ;
          }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,615
    I was using cout.
    You forgot to use it to print what you wanted to print though, glad to see that you have it fixed.
    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

Similar Threads

  1. Windows Picture and Fax Viewer Problemo
    By The Brain in forum Tech Board
    Replies: 0
    Last Post: 02-08-2006, 04:41 PM
  2. A little GDI/Msg type problemo
    By Mithoric in forum Windows Programming
    Replies: 3
    Last Post: 02-21-2004, 07:52 PM
  3. system problemo
    By Stan100 in forum Game Programming
    Replies: 5
    Last Post: 11-15-2002, 02:22 PM
  4. Character Creation Problemo
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 09-15-2002, 10:04 AM

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