Why is my While Loop getting skipped? c++ -
the program skips on while loops , ends. super frustrating. put value of anscheck false right before while loop. no luck. program none of in while loop. here's relevant code:
bool anscheck; anscheck = false; while (anscheck = false) { getline(cin, ans1); if (ans1 != "t" || ans1 != "f") { cout << "please enter t true or f false" << endl; cout << "answer not t or not f" << endl; // debugging } else { anscheck = true; cout << "changed bool true" << endl; } }
you need use comparison operator equality ==
instead of assignment operator =
.
while (anscheck == false) { // ... }
also, mentioned in comment below answer, condition in if statement never being evaluated true. compare strings, should use strcmp
, returns 0 when contents of both c-strings equal. see this reference more information.
if (strcmp(ans1, "t") != 0 && strcmp(ans1, "f") != 0) { // ... }
Comments
Post a Comment