### Make a Reset() Function

I just finished making a hangman game and I can't figure out how to make a reset function

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287`` ``````#include #include #include #include #include void PlayGame(); void Rules(); std::string CreateHiddenWord(const std::string &SelectedTopic); void PrintMan(); void PrintTopic(const std::string &SelectedTopic); void PrintUnderscore(const std::string &HiddenWord, std::vector &ReplaceUnderscore); void CheckGameStatus(const std::vector &ReplaceUnderscore, const std::string &HiddenWord); std::string InputGuess(); std::vector TestGuess(const std::string &Guess, const std::string &HiddenWord, std::vector &ReplaceUnderscore); void GameWon(const std::string &HiddenWord); void GameLost(const std::string &HiddenWord); void AskToPlayAgain(); void Reset(); int Errors = 0; int main() { srand((unsigned int(time(NULL)))); PlayGame(); return 0; } void PlayGame() { std::vector ReplaceUnderscore; const std::string Topic[] = { "Animals", "Football", "C++" }; const std::string SelectedTopic = Topic[rand() % 3]; std::string HiddenWord = CreateHiddenWord(SelectedTopic); Rules(); while (true) { PrintMan(); PrintTopic(SelectedTopic); PrintUnderscore(HiddenWord, ReplaceUnderscore); CheckGameStatus(ReplaceUnderscore, HiddenWord); std::string Guess = InputGuess(); ReplaceUnderscore = TestGuess(Guess, HiddenWord, ReplaceUnderscore); std::cout << std::flush; system("CLS"); } } void Rules() { std::cout << "Basic Rules: 1. 7 Chances For Errors. 2. Only use lowercase. 3. Use only 1 letter.\n\n"; } std::string CreateHiddenWord(const std::string &SelectedTopic) { std::string HiddenWord; const std::string RandomAnimal[] = { "dog", "cat", "elephant", "monkey", "bird", "panda", "snake", "donkey" }; const std::string RandomFootball[] = { "gators", "bulldogs", "patriots", "football", "kickoff", "quarter" }; const std::string RandomCPP[] = { "switch", "for", "while", "vector", "class", "pointer", "array", "header" }; switch (SelectedTopic[0]) { case 'A': HiddenWord = RandomAnimal[rand() % 8]; break; case 'F': HiddenWord = RandomFootball[rand() % 6]; break; case 'C': HiddenWord = RandomCPP[rand() % 8]; break; } return HiddenWord; } void PrintMan() { switch (Errors) { case 0: std::cout << " #######\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 1: std::cout << " #######\n"; std::cout << " ## |\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 2: std::cout << " #######\n"; std::cout << " ## |\n"; std::cout << " ## @\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 3: std::cout << " #######\n"; std::cout << " ## |\n"; std::cout << " ## @\n"; std::cout << " ## |\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 4: std::cout << " #######\n"; std::cout << " ## | \n"; std::cout << " ## @ \n"; std::cout << " ## -| \n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 5: std::cout << " #######\n"; std::cout << " ## | \n"; std::cout << " ## @ \n"; std::cout << " ## -|- \n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 6: std::cout << " #######\n"; std::cout << " ## | \n"; std::cout << " ## @ \n"; std::cout << " ## -|- \n"; std::cout << " ## / \n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 7: std::cout << " #######\n"; std::cout << " ## | \n"; std::cout << " ## @ \n"; std::cout << " ## -|-\n"; std::cout << " ## / \\\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; } std::cout << "\n\n"; } void PrintTopic(const std::string &SelectedTopic) { std::cout << "Topic = " << SelectedTopic; std::cout << "\n\n"; } void PrintUnderscore(const std::string &HiddenWord, std::vector &ReplaceUnderscore) { std::vector Output; if (ReplaceUnderscore.size() > 0) { if (ReplaceUnderscore.size() == 1) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (ReplaceUnderscore[0] == HiddenWord[i]) { std::cout << HiddenWord[i] << " "; } else { std::cout << "_ "; } } } else { for (size_t i = 0; i < HiddenWord.length(); i++) { Output.push_back('_'); } for (size_t j = 0; j < ReplaceUnderscore.size(); j++) { for (size_t i = 0; i < Output.size(); i++) { if (HiddenWord[i] == ReplaceUnderscore[j]) { Output[i] = ReplaceUnderscore[j]; } } } for (size_t i = 0; i < Output.size(); i++) { std::cout << Output[i] << " "; } } } else { for (size_t i = 0; i < HiddenWord.length(); i++) { std::cout << "_ "; } } std::cout << "\n\n"; } void CheckGameStatus(const std::vector &ReplaceUnderscore, const std::string &HiddenWord) { std::string VectorString(ReplaceUnderscore.begin(), ReplaceUnderscore.end()); int Repeat = 0; int Correct = 0; for (size_t j = 0; j < HiddenWord.length(); j++) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (HiddenWord[j] == HiddenWord[i] && j != i) { Repeat += 1; } } } for (size_t j = 0; j < VectorString.length(); j++) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (VectorString[j] == HiddenWord[i]) { Correct += 1; } } } Correct -= Repeat; if (Correct == VectorString.length() && Correct == HiddenWord.length()) { GameWon(HiddenWord); } else if (Errors == 7) { GameLost(HiddenWord); } } std::string InputGuess() { std::string Guess; std::cout << "Enter your guess: "; std::getline(std::cin, Guess); return Guess; } std::vector TestGuess(const std::string &Guess, const std::string &HiddenWord, std::vector &ReplaceUnderscore) { int Correct = 0; for (size_t i = 0; i < HiddenWord.length(); i++) { if (HiddenWord[i] == Guess[0]) { ReplaceUnderscore.push_back(HiddenWord[i]); Correct += 1; } } if (Correct == 0) { Errors += 1; } return ReplaceUnderscore; } void GameWon(const std::string &HiddenWord) { std::cout << "You won! Hidden word was " << HiddenWord; AskToPlayAgain(); } void GameLost(const std::string &HiddenWord) { std::cout << "You lost! Hidden word was " << HiddenWord; AskToPlayAgain(); } void AskToPlayAgain() { std::string Choice; std::cout << "\n\nDo you want to play again? [Y/N]: "; std::cin >> Choice; switch (Choice[0]) { case 'y': case 'Y': break; case 'n': case 'N': exit(0); break; default: std::cout << "\n\nIncorrect answer, try again."; AskToPlayAgain(); break; } Reset(); PlayGame(); } void Reset() { // ???????????????????///// std::cout << std::flush; system("CLS"); }``````
Last edited on
Reset what?

You need to have a better structure, so sub-functions return a status back up the call chain.
Endless recursion won't work in the long run.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305`` ``````#include #include #include #include #include void PlayGame(); void Rules(); std::string CreateHiddenWord(const std::string & SelectedTopic); void PrintMan(); void PrintTopic(const std::string & SelectedTopic); void PrintUnderscore(const std::string & HiddenWord, std::vector < char >&ReplaceUnderscore); bool CheckGameStatus(const std::vector < char >&ReplaceUnderscore, const std::string & HiddenWord); std::string InputGuess(); std::vector < char >TestGuess(const std::string & Guess, const std::string & HiddenWord, std::vector < char >&ReplaceUnderscore); void GameWon(const std::string & HiddenWord); void GameLost(const std::string & HiddenWord); bool AskToPlayAgain(); void Reset(); int Errors = 0; int main() { srand((unsigned int)time(NULL)); //!! This didn't even compile as you posted it. do { PlayGame(); } while ( AskToPlayAgain() ); return 0; } void PlayGame() { std::vector < char >ReplaceUnderscore; const std::string Topic[] = { "Animals", "Football", "C++" }; const std::string SelectedTopic = Topic[rand() % 3]; std::string HiddenWord = CreateHiddenWord(SelectedTopic); Rules(); bool gameOver = false; while ( !gameOver ) { PrintMan(); PrintTopic(SelectedTopic); PrintUnderscore(HiddenWord, ReplaceUnderscore); std::string Guess = InputGuess(); ReplaceUnderscore = TestGuess(Guess, HiddenWord, ReplaceUnderscore); gameOver = CheckGameStatus(ReplaceUnderscore, HiddenWord); } } void Rules() { std::cout << "Basic Rules: 1. 7 Chances For Errors. 2. Only use lowercase. 3. Use only 1 letter.\n\n"; } std::string CreateHiddenWord(const std::string & SelectedTopic) { std::string HiddenWord; const std::string RandomAnimal[] = { "dog", "cat", "elephant", "monkey", "bird", "panda", "snake", "donkey" }; const std::string RandomFootball[] = { "gators", "bulldogs", "patriots", "football", "kickoff", "quarter" }; const std::string RandomCPP[] = { "switch", "for", "while", "vector", "class", "pointer", "array", "header" }; switch (SelectedTopic[0]) { case 'A': HiddenWord = RandomAnimal[rand() % 8]; break; case 'F': HiddenWord = RandomFootball[rand() % 6]; break; case 'C': HiddenWord = RandomCPP[rand() % 8]; break; } return HiddenWord; } void PrintMan() { switch (Errors) { case 0: std::cout << " #######\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 1: std::cout << " #######\n"; std::cout << " ## |\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 2: std::cout << " #######\n"; std::cout << " ## |\n"; std::cout << " ## @\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 3: std::cout << " #######\n"; std::cout << " ## |\n"; std::cout << " ## @\n"; std::cout << " ## |\n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 4: std::cout << " #######\n"; std::cout << " ## | \n"; std::cout << " ## @ \n"; std::cout << " ## -| \n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 5: std::cout << " #######\n"; std::cout << " ## | \n"; std::cout << " ## @ \n"; std::cout << " ## -|- \n"; std::cout << " ##\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 6: std::cout << " #######\n"; std::cout << " ## | \n"; std::cout << " ## @ \n"; std::cout << " ## -|- \n"; std::cout << " ## / \n"; std::cout << " ##\n"; std::cout << " #########\n"; break; case 7: std::cout << " #######\n"; std::cout << " ## | \n"; std::cout << " ## @ \n"; std::cout << " ## -|-\n"; std::cout << " ## / \\\n"; std::cout << " ##\n"; std::cout << " #########\n"; break; } std::cout << "\n\n"; } void PrintTopic(const std::string & SelectedTopic) { std::cout << "Topic = " << SelectedTopic; std::cout << "\n\n"; } void PrintUnderscore(const std::string & HiddenWord, std::vector < char >&ReplaceUnderscore) { std::vector < char >Output; if (ReplaceUnderscore.size() > 0) { if (ReplaceUnderscore.size() == 1) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (ReplaceUnderscore[0] == HiddenWord[i]) { std::cout << HiddenWord[i] << " "; } else { std::cout << "_ "; } } } else { for (size_t i = 0; i < HiddenWord.length(); i++) { Output.push_back('_'); } for (size_t j = 0; j < ReplaceUnderscore.size(); j++) { for (size_t i = 0; i < Output.size(); i++) { if (HiddenWord[i] == ReplaceUnderscore[j]) { Output[i] = ReplaceUnderscore[j]; } } } for (size_t i = 0; i < Output.size(); i++) { std::cout << Output[i] << " "; } } } else { for (size_t i = 0; i < HiddenWord.length(); i++) { std::cout << "_ "; } } std::cout << "\n\n"; } bool CheckGameStatus(const std::vector < char >&ReplaceUnderscore, const std::string & HiddenWord) { bool gameOver = false; std::string VectorString(ReplaceUnderscore.begin(), ReplaceUnderscore.end()); int Repeat = 0; int Correct = 0; for (size_t j = 0; j < HiddenWord.length(); j++) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (HiddenWord[j] == HiddenWord[i] && j != i) { Repeat += 1; } } } for (size_t j = 0; j < VectorString.length(); j++) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (VectorString[j] == HiddenWord[i]) { Correct += 1; } } } Correct -= Repeat; if (Correct == VectorString.length() && Correct == HiddenWord.length()) { GameWon(HiddenWord); gameOver = true; } else if (Errors == 7) { gameOver = true; GameLost(HiddenWord); } return gameOver; } std::string InputGuess() { std::string Guess; std::cout << "Enter your guess: "; std::getline(std::cin, Guess); return Guess; } std::vector < char >TestGuess(const std::string & Guess, const std::string & HiddenWord, std::vector < char >&ReplaceUnderscore) { int Correct = 0; for (size_t i = 0; i < HiddenWord.length(); i++) { if (HiddenWord[i] == Guess[0]) { ReplaceUnderscore.push_back(HiddenWord[i]); Correct += 1; } } if (Correct == 0) { Errors += 1; } return ReplaceUnderscore; } void GameWon(const std::string & HiddenWord) { std::cout << "You won! Hidden word was " << HiddenWord; } void GameLost(const std::string & HiddenWord) { std::cout << "You lost! Hidden word was " << HiddenWord; } bool AskToPlayAgain() { bool result = false; bool again = true; do { std::string Choice; std::cout << "\n\nDo you want to play again? [Y/N]: "; std::cin >> Choice; switch (Choice[0]) { case 'y': case 'Y': result = true; again = false; break; case 'n': case 'N': result = false; again = false; break; default: std::cout << "\n\nIncorrect answer, try again."; break; } } while ( again ); return result; } void Reset() { // ???????????????????///// std::cout << std::flush; system("CLS"); }``````
Rather than passing parameters all over, create a Hangman class that contains the hidden word and replaceUnderscore. Then, if you make AskToPlayAgain a function outside the class, you can easily reset all the data by creating a new Hangman object each time you want to play the game:
 ``123456789101112131415161718192021222324`` ``````class Hangman { public: std::string HiddenWord; std::vector ReplaceUnderscore; void PlayGame(); void Rules(); void CreateHiddenWord(const std::string & SelectedTopic); void PrintMan(); void PrintTopic(const std::string & SelectedTopic); void PrintUnderscore(); bool CheckGameStatus(); std::string InputGuess(); void TestGuess(const std::string & Guess); void GameWon(); void GameLost(); void Reset(); int Errors = 0; }; do { Hangman hangman; hangman.PlayGame(); } while (AskToPlayAgain());``````

I've somewhat arbitrarily left SelectedTopic and Guess as local variables that are passed as parameters when needed. Others might prefer to make those class members too.

In general, data is much smaller than code. In printMap, just create an array of cstrings holding the characters to print for each man:
 ``1234567891011`` `````` static const char *men[] = { " #######\n" // 0 " ##\n" " ##\n" " ##\n" " ##\n" " ##\n" " #########\n", ... }; std::cout << men[Errors] << "\n\n";``````

I've used a lesser-known feature of C++ here: you can continue a string literal on the next line by just restarting it:
 ``12`` ``````"Hello " "World"``````

is the same as
`"Hello World"`

Building on salem c's code. I've left the Reset() method, although I don't think you need it.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313`` ``````#include #include #include #include #include class Hangman { public: std::string HiddenWord; std::vector ReplaceUnderscore; void PlayGame(); void Rules(); void CreateHiddenWord(const std::string & SelectedTopic); void PrintMan(); void PrintTopic(const std::string & SelectedTopic); void PrintUnderscore(); bool CheckGameStatus(); std::string InputGuess(); void TestGuess(const std::string & Guess); void GameWon(); void GameLost(); void Reset(); int Errors = 0; }; // AskToPlayAgain() isn't part of the game itself. This way You can // construct a new game object each time they want to play. See main() bool AskToPlayAgain(); int main() { srand((unsigned int) time(NULL)); //!! This didn't even compile as you posted it. do { Hangman hangman; hangman.PlayGame(); } while (AskToPlayAgain()); return 0; } void Hangman::PlayGame() { const std::string Topic[] = { "Animals", "Football", "C++" }; const std::string SelectedTopic = Topic[rand() % 3]; CreateHiddenWord(SelectedTopic); Rules(); bool gameOver = false; while (!gameOver) { PrintMan(); PrintTopic(SelectedTopic); PrintUnderscore(); std::string Guess = InputGuess(); TestGuess(Guess); gameOver = CheckGameStatus(); } } void Hangman::Rules() { std:: cout << "Basic Rules: 1. 7 Chances For Errors. 2. Only use lowercase. 3. Use only 1 letter.\n\n"; } void Hangman::CreateHiddenWord(const std::string & SelectedTopic) { const std::string RandomAnimal[] = { "dog", "cat", "elephant", "monkey", "bird", "panda", "snake", "donkey" }; const std::string RandomFootball[] = { "gators", "bulldogs", "patriots", "football", "kickoff", "quarter" }; const std::string RandomCPP[] = { "switch", "for", "while", "vector", "class", "pointer", "array", "header" }; switch (SelectedTopic[0]) { case 'A': HiddenWord = RandomAnimal[rand() % 8]; break; case 'F': HiddenWord = RandomFootball[rand() % 6]; break; case 'C': HiddenWord = RandomCPP[rand() % 8]; break; } } void Hangman::PrintMan() { static const char *men[] = { " #######\n" // 0 " ##\n" " ##\n" " ##\n" " ##\n" " ##\n" " #########\n", " #######\n" // 1 " ## |\n" " ##\n" " ##\n" " ##\n" " ##\n" " #########\n", " #######\n" // 2 " ## |\n" " ## @\n" " ##\n" " ##\n" " ##\n" " #########\n", " #######\n" // 3 " ## |\n" " ## @\n" " ## |\n" " ##\n" " ##\n" " #########\n", " #######\n" // 4 " ## | \n" " ## @ \n" " ## -| \n" " ##\n" " ##\n" " #########\n", " #######\n" // 5 " ## | \n" " ## @ \n" " ## -|- \n" " ##\n" " ##\n" " #########\n", " #######\n" // 6 " ## | \n" " ## @ \n" " ## -|- \n" " ## / \n" " ##\n" " #########\n", " #######\n" // 7 " ## | \n" " ## @ \n" " ## -|-\n" " ## / \\\n" " ##\n" " #########\n" }; std::cout << men[Errors] << "\n\n"; } void Hangman::PrintTopic(const std::string & SelectedTopic) { std::cout << "Topic = " << SelectedTopic; std::cout << "\n\n"; } void Hangman::PrintUnderscore() { std::vector < char >Output; if (ReplaceUnderscore.size() > 0) { if (ReplaceUnderscore.size() == 1) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (ReplaceUnderscore[0] == HiddenWord[i]) { std::cout << HiddenWord[i] << " "; } else { std::cout << "_ "; } } } else { for (size_t i = 0; i < HiddenWord.length(); i++) { Output.push_back('_'); } for (size_t j = 0; j < ReplaceUnderscore.size(); j++) { for (size_t i = 0; i < Output.size(); i++) { if (HiddenWord[i] == ReplaceUnderscore[j]) { Output[i] = ReplaceUnderscore[j]; } } } for (size_t i = 0; i < Output.size(); i++) { std::cout << Output[i] << " "; } } } else { for (size_t i = 0; i < HiddenWord.length(); i++) { std::cout << "_ "; } } std::cout << "\n\n"; } bool Hangman::CheckGameStatus() { bool gameOver = false; std::string VectorString(ReplaceUnderscore.begin(), ReplaceUnderscore.end()); int Repeat = 0; unsigned Correct = 0; for (size_t j = 0; j < HiddenWord.length(); j++) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (HiddenWord[j] == HiddenWord[i] && j != i) { Repeat += 1; } } } for (size_t j = 0; j < VectorString.length(); j++) { for (size_t i = 0; i < HiddenWord.length(); i++) { if (VectorString[j] == HiddenWord[i]) { Correct += 1; } } } Correct -= Repeat; if (Correct == VectorString.length() && Correct == HiddenWord.length()) { GameWon(); gameOver = true; } else if (Errors == 7) { gameOver = true; GameLost(); } return gameOver; } std::string Hangman::InputGuess() { std::string Guess; std::cout << "Enter your guess: "; std::getline(std::cin, Guess); return Guess; } void Hangman::TestGuess(const std::string & Guess) { int Correct = 0; for (size_t i = 0; i < HiddenWord.length(); i++) { if (HiddenWord[i] == Guess[0]) { ReplaceUnderscore.push_back(HiddenWord[i]); Correct += 1; } } if (Correct == 0) { Errors += 1; } } void Hangman::GameWon() { std::cout << "You won! Hidden word was " << HiddenWord; } void Hangman::GameLost() { std::cout << "You lost! Hidden word was " << HiddenWord; } bool AskToPlayAgain() { bool result = false; bool again = true; do { std::string Choice; std::cout << "\n\nDo you want to play again? [Y/N]: "; std::cin >> Choice; switch (Choice[0]) { case 'y': case 'Y': result = true; again = false; break; case 'n': case 'N': result = false; again = false; break; default: std::cout << "\n\nIncorrect answer, try again."; break; } } while (again); return result; } void Hangman::Reset() { // ???????????????????///// std::cout << std::flush; system("CLS"); }``````

Hello ChimpCoder,

Generally with a "reset" function one would think that there would be several variables that need reset.

Based on the way the program flows most variables loose scope when the function ends or functions are being called, so its like starting new taking care of most of the variables.

The variable "Errors" is defined as a global variable. This works, but you should try to avoid global variables like this. Something that starts as "constexpr" or "const" are OK because as a constant they can not be changed by the program.

In this program it looks like you define "Errors" and then forgot about it. So when the programs goes to play another game "Errors" already has a value, so your second game starts with errors even before you make a guess.

Because of the program flow I had to reset "Errors" to "-1" because it looks like function "TestGuess" is being called before it should be starting game two with one incorrect answer when there should be none.

The function "AskToPlayAgain" calls "PlayGame" before you even return from when PlayGame" was first called.

Thinking about the program flow "main" should call "PlayGame" and when that function returns back to main the function "AskToPlayAgain" should be called and not after you printed a win or loss. This way you will not have to use the "exit()" as you did and when functions end properly the variables defined there also end meaning that they do not have to be reset.

I still want to do some testing, but for now I am thinking:
 ``12345678910111213`` ``````int main() { srand((unsigned int(time(NULL)))); bool contGame{ true }; while (contGame) { PlayGame(); AskToPlayAgain(contGame); } return 0; }``````

And the "AskToPlayAgain" functiom could be something like:
 ``123456789101112131415161718192021222324252627282930`` ``````void AskToPlayAgain(bool& contGame) { std::string Choice; do { std::cout << "\n\nDo you want to play again? [Y/N]: "; std::cin >> Choice; switch (Choice[0]) { case 'y': case 'Y': Reset(); // <--- Moved to here. break; case 'n': case 'N': contGame = false; break; default: std::cout << "\n\nIncorrect answer, try again."; //AskToPlayAgain(contGame);// <--- Do not call the function. use a do/while loop around the switch. break; } } while (std::tolower(Choice[0]) != 'y' || std::tolower(Choice[0]) != 'n'); //PlayGame(); // <--- Do not call from here. }``````

All you need for "Choice" is a "char" not a "string". Note: "contGame" is is being passed by reference. Or you could have the function return a bool. Eithe way works.

In "PlayGame' I would set up the while in much the same way as I showed you in "main" although I would most likely call the variable "cont" and then pass it be reference to the function(s) that eventually get to the functiions "GameWon" and "GameLost" where you would set "cont" to false.

The point is that you need to end functions before you call them for a second time.

Hope that helps,

Andy
Hello ChimpCoder,

I am not sure what I was thinking at the time, but I found that I had to change the while condition of the "AskToPlayAgain" function from "||" to "&&". It should look like this:
`while (std::tolower(choice) !='y' && std::tolower(choice) != 'n');` I also changed "Choice" to "char choice".