It is a part of code I am working on for assignment. When I execute the file, it
ID: 3549862 • Letter: I
Question
It is a part of code I am working on for assignment. When I execute the file, it asks if I am watching the game or not. Then I press Yes, yes, No or no. But it keeps asking me "Please enter only Yes or No: " no matter what I type in.
What is the problem???
1. int watch_stat() {
2. string watch; //prompting watch status
3. cout << "Would you like to watch the game? (Yes or No) :";
4. cin >> watch;
5. while (watch != "Yes" || watch != "yes" || watch != "no" || watch != "No") {
6. cin.clear();
7. cin.ignore(100, ' ');
8. cout << "Please enter only Yes or No :";
9. cin >> watch;
10. }
11. int watchreturn;
12. if (watch == "Yes" || watch == "yes") {
13. watchreturn = 1;
14. }
15. else {
16. watchreturn = 0;
17. }
18. return watchreturn;
19. }
Explanation / Answer
please replace
while (watch != "Yes" || watch != "yes" || watch != "no" || watch != "No")
with
while (watch != "Yes" && watch != "yes" && watch != "no" && watch != "No")
i.e the while loop condition should be true if
watch is not Yes and watch is not yes and watch is not no and watch is not No
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.