You need to decide exactly what the input you require from the user is and what makes it valid. It sounds to me like you want to accept an entire line of input from the user (so you should use std::getline
with a std::string
) and then you want to only accept strings that are exactly "+"
and "-"
.
std::string sign;
std::getline(std::cin, sign); // Gather input
if (sign != "+" && sign != "-") { // Parse input
// ...
}
// ...
As @H2C03 mentioned, it's a good idea to split up the inputting and the parsing. In this case, the parsing is as simple as comparing the input line to the strings "+"
and "-"
.