Help with If statement

When the user inputs : a + b = c
It still prints " you are missing the Operand"...but Why?
I know the delimiter does not get included; in this case the delimiter is ' '. Could use some Help, Thanks

1
2
3
4
5
6
7
8
9
10
11
  getline(cin, word1,' ');
	getline(cin, operand, ' ');
	if(operand != "+" || operand != "-" || operand != "*" || operand != "/") {
		cout << "you are missing the Operand" << endl;
	}
	getline(cin, word2, ' ');
	getline(cin, equal, ' ');
	if (equal != "=") {
		cout << " Missing equal sign" << endl;
	}
	getline(cin, word3);
Last edited on
At line 3 you want "and" operationa (&&) instead of "or" operations (||)

To see this, remember that with the || operation, if either operand is true, then the whole expression is true. So for operand != "+" || operand != "-" to be false, then it must be that operand is "+" and it also is "-". Clearly it can't be both.

Put another way, if operand is "+" then operand != "-" is true. And if operand is "-" then operand != "+" is true.
Thanks@ dhayden

for me to ask the user to repeat the equation if they missed an operand, why wouldnt this work?:
1
2
3
4
5
6
7
8
9
10
11
cout << "Enter 3 words that do not exceed 10 unique letters (a + b = c):" << endl;
	getline(cin, word1,' ');
	getline(cin, operand, ' ');
	while(operand != "+" && operand != "-" && operand != "*" && operand != "/") {
		cout << "you are missing the Operand" << endl;
		getline(cin, word1, ' ');
		getline(cin, operand, ' ');
		getline(cin, word2, ' ');
		getline(cin, equal, ' ');
		getline(cin, word3);
	}
Hi,

Btw I have a pathological hatred (I mean that in the nicest possible way :+) ) of conditions like this:

operand != "+" && operand != "-" && operand != "*" && operand != "/"

Better to use std::string or some other container to store the operators in, then if std::find fails the input is invalid.

I found this about parsing:

https://en.wikipedia.org/wiki/Shunting-yard_algorithm


I thought it might be a better methodology :+)
closed account (zv05oG1T)
Try to use '+' instead of "+". When you have only one character, you have to use ' '.
1
2
3
4
5
6
7
8
9
10
11
cout << "Enter 3 words that do not exceed 10 unique letters (a + b = c):" << endl;
	getline(cin, word1,' ');
	getline(cin, operand, ' ');
	while(operand != '+' && operand != '-' && operand != '*' && operand != '/') {
		cout << "you are missing the Operand" << endl;
		getline(cin, word1, ' ');
		getline(cin, operand, ' ');
		getline(cin, word2, ' ');
		getline(cin, equal, ' ');
		getline(cin, word3);
	}
Last edited on
Topic archived. No new replies allowed.