# Beginner in Trouble!

• 10-22-2004
Sarina
Beginner in Trouble!
Hi I'm stuck on this very simple programme. The user is prompted to enter an equation and, if it is not in the format x + y = z I would like it to output an error message. However, if someone typed in 2 + 3 = 3 + 2, the addition 3 + 2 is automatically done and the value of 5 goes into z.

If anyone can help me, I'm very confused!

Thanks!

Code:

``` #include <iostream> #include <string> using namespace std; int main () {        cout << "Please enter an equation."<<endl;                 int x, y, result;         string sign, equals;     cin >> x >> sign >> y >> equals >> result;         if         ((cin.fail ()) || (sign != "+") && (sign != "-"))         {cout << "Invalid Input";                 return 1;}              bool plus = false;     if         (sign == "+") plus = true;                //check for addition/minus             {             if               (plus)                 {if                 (x+y ==  result)                       cout << "Correct1";                     else                       cout << "Incorrect1";                         }               else   {if           (x-y == result)           cout << "Correct2";           else           cout << "Incorrect2";           }  }```
• 10-22-2004
JaWiB
Hm so one problem I see is that if you are going to allow 2+3 on the right side, then you need some sort of loop to continue inputting data until the user wants it to stop...
Also, if you allow more than one term on the right, then why not more than two on the left (I guess it's ok, just a bit strange)

Looking at your code, I can see a problem here (the indents needed fixing!):
Code:

```if (sign == "+") plus = true;                //check for addition/minus             { //not what you want maybe   if (plus)   {     if (x+y ==  result)       cout << "Correct1";   else       cout << "Incorrect1";   }   else   {       if (x-y == result)         cout << "Correct2";       else         cout << "Incorrect2";   }```
I think you wanted:
Code:

```if (sign == "+") { //  if (plus) no reason for this variable // { //... } else {   if (x-y == result)       cout << "Correct2";   else       cout << "Incorrect2"; }```
Start by fixing that and see how it works
• 10-22-2004
Sarina
Thanks for that - I realised that the bool thing was kinda pointless. But you know...you read something knew and you want to use it!

I dont think that I'm very good at explaining things in the confines of one box so I will grab a friend this weekend and yell them down until I make sense.

Thanks again!
• 10-23-2004
india82
But how to make the sum on the right side invalid?
For example, to only allow 1 + 1 = 2. So, if 1 + 1 = 2 - 1 is entered by the user, despite being correct, an error message will be generated. In other words, how to output an error message if anything other than the format x + y = z (or x - y = z) is entered?

Thanks.
• 10-23-2004
Epo
Could you read their equation all in as one string to start, then mess around with that?

For example, BigString = "2+5=8-1"

And then have smaller strings that will hold each side of the equal sign (so a LeftSideString and a RightSideString).

If the RightSideString had a length greater than 1, you'd know they input something wrong.

And you could still use the method you have above to evaluate the left side.

It might be more trouble than it's worth, but that's the first idea that comes to my mind...