Hello,
I am new to this website, and I am seeking for a little bit of help.
I am studying C++ and I am facing some sort of a problem with the loop function.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//Variables
string choose;
int number1;
int number2;
//Output
cout << "This program is written by: \n";
cout << '\n';
cout << "What do you want to do: Subtract, Add, Multiply, Divide?\n";
cout << "I want to: ";
cin >> choose;
cout << '\n';
//loop
while( choose != "Add" || choose != "Subtract" || choose != "Multiply" || choose != "Divide" )
{
cout << '\n';
cout << "Input not recognized, try again\n";
cout << "What do you want to do: Subtract, Add, Multiply, Divide?\n";
cout << "I want to: ";
cin >> choose;
}
//If the user chooses to subtract
if(choose == "Subtract")
{
cout << "It seems you have chosen to Subtract, Choose your first number: ";
cin >> number1;
cout << "Now choose your second number: ";
cin >> number2;
}
cin.get();
}
So, I tried the loop, and if I don't input one of strings "Add, Subtract, Multiply or Divide" it loops again (So far everything is fine), so I have to tried to input the right string "Subtract", but instead of displaying the if function, it displays the loop function once again, instead of the if function.
Well, your condition is "if choose is not equal to "Add" or if choose is not equal to "Subtract"..."
choose can't equal all of those at the same time, so the condition is always true.
Logical and would be the right operator to use here.