C++ homework; using strings and math

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-28-2010
UWP student
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.
• 09-28-2010
UWP student
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.
• 09-28-2010
rogster001
you mean you are not allowed to just do -if first number equals zero then let number1 be 0 ?
• 09-28-2010
UWP student
no because it says "cannot assign values of type const char(2) to a variable type int
• 09-28-2010
laserlight
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.
• 09-28-2010
UWP student
that works laserlight, but then do I have to do that for every possible equation.
0+1
0+2
0*1.....and so on?
• 09-28-2010
laserlight
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.
• 09-28-2010
UWP student
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?
• 09-28-2010
UWP student
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
• 09-28-2010
laserlight
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".
• 09-28-2010
UWP student
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?
• 09-28-2010
laserlight
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.
• 09-28-2010
rogster001
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
• 09-28-2010
UWP student
my problem is I do not know how to assign things
• 09-28-2010
laserlight
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; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last