I am making a quiz, and I am having trouble with this code block:
string answer2;
cout << "PC stands for: " << endl;
getline (cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), toLower());
if (answer2 == "personal computer" || answer2== "computer" || answer2 == "personal"); //should only accept these answers
cout << "Correct!" << endl;
For some reason it accepts asd as an answer when it shouldn't! How can I get this to stop? let me know if I am unclear, need to post more code, etc.
I know now an else statement is needed. Should it go under the cout << "correct " << endl; ?
Try using
compare( )[1]. You can use it like this:
1 2 3 4 5 6 7 8
|
string Answer;
getline( cin, Answer );
if( Answer.compare( "personal computer" ) )
{
// Do something...
}
|
References:
[1]http://www.cplusplus.com/reference/string/string/compare/
Wazzak
Last edited on
if ( /* ... */ );
<- remove this semicolon
Last edited on
Also you can use strcmp function.
if it's true, it returns 0.
much thanks m4ster r0shi. I always find that my programs contain some sort of missed ';'. It can be quit frustrating :P