Thread: C++ homework; using strings and math

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    20

    C++ homework; using strings and math

    Hey so this is my first ever post so I hope I am doing it right. I have been working on this homework assignment for over a week and I am so close to being done but I cannot figure out the rest. So any help would be great, and please remember I am new to C++ so this assignment might seem easy to you but it is hard for me.

    Assignment: Write a C++ program that computes the value of a simple arithmeitc expression consisting of two operands (whose respective values can be either 0, 1 or 2) along with one of the standard arithmetic operators (such as +, -, * and /). Your program should input the operands along with the operator as strings.

    my code so far is
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string sign, firstnumber, secondnumber;  
       float value;
       int number1, number2;
                         
                    // Ask user for numbers and operator
              
       cout << "Please enter the first number(zero, one or two): " << endl;
       cin >> firstnumber;
       cout << "Please enter operator(plus, minus, divide, times): " << endl;
       cin >> sign; 
       cout << "Please enter the second number(zero, one or two): " << endl;
       cin >> secondnumber;
       
          
          
              // Solve the expression  
              
         
            if(sign=="plus")
             {
             value = float(number1 + number2);
               if (value > 2)
               cout << "The expression you entered is not valid." << endl;
             else 
             cout << "The value of the expression you entered is " << value << "." << endl;
             }
           else if(sign=="minus")
             {
             value = float(number1 - number);
               if (value > 2)
               cout << "The expression you entered is not valid." << endl;
             else
             cout << "The value of the expression you entered is " << value << "." << endl;
             }
           else if(sign=="times")
             {
             value = float(number1 * number2);
               if (value > 2)
               cout << "The expression you entered is not valid." << endl;
             else
             cout << "The value of the expression you entered is " << value << "." << endl;
             }
            if(sign=="divide")
             {
             value = float(number1 / number2);
               if (value > 2)
               cout << "The expression you entered is not valid." << endl;
             else
             if (value == 0)
             cout << "Error cannot divide by zero." << endl;
             else
             cout << "The value of the expression you entered is " << value << "." << endl;
             }               
    return 0;
    }
    I can make everything work but only if they type in 0, 1 or 2. I need to make it work when they type in zero ,one or two. But if I use the string for the number, I cannot use the string in my equation. So basically how can I convert my string into the equation the program should run like this:

    Enter the first operand (zero, one or two):
    zero
    Enter an arithmetic operator (plus, minus, times, divide):
    times
    Enter the second operand (zero, one or two):
    one
    The value of the expression is 0.

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    the only way I can get all the equations to work is if I change firstnumber and secondnumber to number1 and number2 but then the inputs will not be strings, and that is part of the problem that I have to do.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you mean you are not allowed to just do -if first number equals zero then let number1 be 0 ?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    no because it says "cannot assign values of type const char(2) to a variable type int

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by UWP student
    no because it says "cannot assign values of type const char(2) to a variable type int
    Instead of assigning "0", assign 0.
    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

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    that works laserlight, but then do I have to do that for every possible equation.
    0+1
    0+2
    0*1.....and so on?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not necessarily. You could convert the word version of the number into the corresponding integer value before you begin checking for which operation to apply. This way, you do not duplicate this conversion for each operation.
    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

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    you mean you are not allowed to just do -if first number equals zero then let number1 be 0 ?

    I am confused on what you mean by "let". How would I put that in a C++ function?

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    laserlight that is what I have been trying to do but for some reason I cannot figure out how to do it. I want firstnumber = number1 and secondnumber = number2, so then all of my equations will work the way they are

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by UWP student
    I am confused on what you mean by "let". How would I put that in a C++ function?
    I think you interpreted rogster001 correctly in post #3. "Let" just means "assign". In fact, if you have learnt how to write your own functions, then you should write a function that takes a string (either "zero", "one" or "two") as an argument and returns the corresponding integer value.

    By the way, what you call a "sign" should be call an "operation".
    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

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    yes laserlight, my only question really is how do I assign a string to an int? This is my first program I have ever done so I am sorry I am just confused. Is my program good so far? Is all I have to do is assign firstnumber to number1 so whatever they type in(firstnumber) will go in the equation as number1?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by UWP student
    my only question really is how do I assign a string to an int?
    You don't. You assign an integer value to an integer variable depending on the contents of a string.
    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

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    in your case just use some if/else tests to assign number1 and number2 with the values indicated by firstnumber and secondnumber, then simply do your calculation with number1 number2 in the float() part
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  14. #14
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    my problem is I do not know how to assign things

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by UWP student
    my problem is I do not know how to assign things
    For example:
    Code:
    if (firstnumber == "zero")
    {
        number1 = 0;
    }
    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. Searching within strings
    By Poincare in forum C Programming
    Replies: 3
    Last Post: 04-24-2009, 08:53 PM
  2. Library for math strings
    By Boomba in forum C Programming
    Replies: 4
    Last Post: 10-15-2005, 08:05 PM
  3. Easy Way To Replace Strings...
    By Geolingo in forum C++ Programming
    Replies: 8
    Last Post: 07-28-2003, 12:32 PM
  4. need help on a collection of strings
    By masongray in forum C Programming
    Replies: 2
    Last Post: 12-04-2002, 03:50 PM
  5. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM