Hello,
I can’t find the problem in my code.
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;
}