Beginner in Trouble!

This is a discussion on Beginner in Trouble! within the C++ Programming forums, part of the General Programming Boards category; Hi I'm stuck on this very simple programme. The user is prompted to enter an equation and, if it is ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    3

    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";
    
      	}  
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    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!

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    1
    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.

  5. #5
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. // Trouble launching 2nd thread from MainWindow, beginner //
    By RickTrelles in forum Windows Programming
    Replies: 2
    Last Post: 07-03-2008, 10:43 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Replies: 12
    Last Post: 10-23-2006, 08:45 AM
  4. A beginner in trouble
    By Eldey in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2004, 10:16 AM
  5. trouble with a loop (beginner)
    By Procta in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2003, 10:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21