I decided to just try and make a program that would identify what KIND of expression you entered, not even evaluate it. Just for it to say: “You entered an addition equation. ” when you enter an equation with a value, a plus sign, then another value … and so on for other types of equations. The problem is that no matter what type of equation I enter, whether it is 5 + 5, 5 – 5, 5 * 5, or 5 / 5 I always get a response telling me that I entered a subtraction equation. Any ideas, anyone?

#include <iostream>
using namespace std;

long int x;
long int y;
long int z;
bool expression; 

int main()
{
    cout << "Enter an expression to be evaluated: " << endl;
    cin >> expression;

    if(expression == true + true) /* i.e. if any integer other than zero is
    entered, followed by a plus sign, then another integer other than zero */
    {
    cout << "You entered an addition equation. " << endl;
    }

    if(expression == true/true) /* i.e. if any integer other than zero is
    entered, followed by a division sign, then another integer other than zero */
    {
    cout << "You entered a division equation. " << endl;
    }

    if(expression == true-true) // like above, but with subtraction
    {
    cout << "You entered a subtraction equation. " << endl;
    }

    if(expression == true*true) // and again like above, with multiplication
    {
    cout << "You entered a multiplication equation. " << endl;
    }    

    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    // ^^ is for Dev C++ so the program stays open until I prompt it to exit
    return 0;                  

}

Recommended Answers

All 3 Replies

You're method is definitely not doing what you think it is. It may be "legal" in that it compiles but the logic is far different.

The variable you read from the user is boolean. It only takes the values true or false. However, it seems that when you enter a number via cin, it is recorded as true (1) if the number is 1 and false (0) for everything else (I would think based on the points below that the value would be 1 for every number except an input of zero but it doesn't seem to be the case). So when you are inputting 2+2 or something, it takes the 2 for the input (since it can convert it to a value) but leaves the rest of it in the input stream (so a subsequent call to cin could pick it up). It was not getting your whole equation.

A word about boolean statements. A value of 0 is considered false and a value of anything else is considered true. So if we have the statement:

if(1)
{
   std::cout<<"Hello."<<std::endl;
}
then Hello would be printed.  

It would not be printed for:
if(0)
{
  std::cout<<"Hello."<<std::endl;
}

So in terms of your statements, look at if(expression == true-true) Say we entered in 2+2, so 2 is read into expression as 0. Since subtraction has a high precedence than ==, true - true is evaluated first (as 1 - 1, even though anything other than zero is considered true, the value true is defined as 1), equaling 0, then 0 == 0 is evaluated which is true, then that if statement is true causing the output. To see what happened on the other ones, test them out.

Some of that might be confusing, so ask for clarification if need be.

In terms of a solution to this difficulty, you'll have to do a bit more work to get at the operator. Read in your value as a string. If all you care about is the operator and you know there's only going to be one, then step through your array until you find '+','-','*', or '/'. If your equations are more complicated then you can try splitting them up into the numbers and the operators. For that you could use something like strtol http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

>>expression == true + true

what caused you to think this is correct? What do you think it does?

I thought that would cause the if statement to be triggered if what was entered was a true value, followed by a plus sign, then another true value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.