# Thread: C++ homework; using strings and math

1. Code:
```#include <iostream>
#include <string>
using namespace std;

int main()
{
string operation, 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 >> operation;
cout << "Please enter the second number(zero, one or two): " << endl;
cin >> secondnumber;

// Solve the expression
if(firstnumber == "zero")
{
number1 = 0;
}
else if(firstnumber == "one")
{
number1 = 1;
}
else if(firstnumber == "two")
{
number1 = 2;
}
if(secondnumber == "zero")
{
number2 = 0;
}
else if(secondnumber == "one")
{
number2 = 1;
}
else if(secondnumber == "two")
{
number2 = 2;
}

if(operation=="plus")
{
value = (number1 + number2);
cout << "The value of the expression you entered is " << value << "." << endl;
}
else if(operation=="minus")
{
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(operation=="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(operation=="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;
}```
Wow thanks guys, everything works now once I figure out how to make 1/2=.5...thank you guys so much I have been working on this for over a week and it is due at 930 tonight so thanks again

2. Before you turn that in, you should properly indent the code. Good exercise and oh-so-important.

3. well it looks like I ran into another problem I didnt even know about. If the person typing in the numbers types in Two instead of two, it is suppose to say error..Is there a way to make it so if the word is capitalized it will say error or do I have to assign each one individually?

4. Try
std::transform(input.begin(), input.end(), input.begin(), &std::tolower);
This should make the input (if it's a std::string) into all lowercase.
Though I am not sure you are allowed to store it directly into the same variable.

Oh yes, and include <algorithm> for std::transform and <locale> for std::tolower.

5. Check out the <cctype> standard header. std::tolower could come in handy.

6. it says no such namespace std::tolower

7. this is what the program is suppose to do

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

Enter the first operand (zero, one or two):
Two
Enter an arithmetic operator (plus, minus, times, divide):
plus
Enter the second operand (zero, one or two):
two
The expression that you entered is not valid.

I can do the good part fine, I just cannot do the bad part

8. I also cannot use tolower. We cannot use functions loops or arrays

9. You can actually use tolower, it's just that it actually looks like this:

Code:
`std::transform (aString.begin(), aString.end(), aString.begin(), ::tolower);`
I forget the exact reason why, but it seems to be a problem whenever you want to use anything from C like this (e.g. tolower(), rand()). It's not clear to me why std::anythingFromC does not resolve to a function pointer.

Also, since this was left unresolved by Elysia, if the first three arguments to transform are the same container, it only means that the transformation occurs in place.

10. Originally Posted by UWP student
I also cannot use tolower. We cannot use functions loops or arrays
In that case, what you want to do is not possible.

Popular pages Recent additions