I can't find the problem in my code (compilation is ok)

urc_s02_booleans_and_comparisons

#1

Hello,

I can’t find the problem in my code. :face_with_raised_eyebrow:
Basically in a function to (re)start the game (bool Replay() ) I want to check if the user answers “Yes”, “yes”, “No”, “no”. If he answers something else, I ask him to do a choice again by repeating the previous input step with the loop.
Else, I continue and things happen based on what the user typed.

My problem is that no matters what I type, I can’t exit my loop.

Here is the code :

#include <iostream>
#include <string>

constexpr int WORD_LENGTH = 5;


void PrintIntro()
{
	std::cout << "Welcome to Bulls and Cows, a word game!" << std::endl;
	std::cout << std::endl;														//alternative à la methode du dessus pour aller a la ligne
	std::cout << "Can you guess the X letter isogram?\n\n";						// \n est aussi une alternative pour aller a la ligne

}


std::string UserInput()
{
	std::string input = "";
	std::getline(std::cin, input);
	return input;
}


bool Replay()
{
	//Entree utilisateur
	std::cout << "\nDo you want to (re)start the game? Yes/No : ";
	std::string restart = "";

	
	while ((restart != ("Yes")) || (restart != ("yes")) || (restart != ("No")) || (restart != ("no")))
	{
		restart = UserInput();
		std::cout << restart;

		if ((restart != ("Yes")) || (restart != ("yes")) || (restart != ("No")) || (restart != ("no")))
		{
			std::cout << "\nPlease enter a valid choice (Yes/No) : ";
		}
	}
	

	//Decision
	if ((restart == "Yes") || (restart == "yes"))
	{
		return true;
	}
	else
	{
		return false;
	}
}


int main()
{
	//(Re)jouer?
	if (Replay() == true)
	{
		PrintIntro();
	}
	else
	{
		std::cout << "\nGood bye!\n\n";
	}

	return 0;
}


#2

Hi, I tried to write on your code commentig the things i’ve done and the things I’ve eliminated from the code. I hope this could be halpful to you.
P.S.
My knowledge of c++ is stuck to this lesson of the course so excuse me if my explenetion are not complete or wrong.
Greetings from Italy :slight_smile:


#3

Thanks!
Actually the problem was that I misunderstood the logic table of the operator || (OR). I just had to replace the || by &&.