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.