I am making a program that gives the user a certain number of tries to guess a hidden word. The compiler outputs the expected behavior except when it comes to the askToPlayAgain() function in "main.cpp".
After the user has exceeded their number of guesses, the program asks the user if they want to play again. If they select "yes", the game, playGame(), resets. If they select "no", the program quits. When the user inputs any string other than "yes" or "no", the program asks the user to please select either "yes" or "no" and then gives them an opportunity to do so again. For all three of these options, the program behaves as expected. However, if the user selects an input other than "yes" or "no" and THEN selects "no" after being prompted to input a valid string, the program does not quit as expected. Instead, the game resets.
Does anyone know why this is happening? Does it have something to do with askToPlayAgain() calling itself? Something to do with how the subroutine is loaded in to memory and the initial subroutine being denied the requirement of returning a boolean?
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include "BullCowGame.h"
usingnamespace std;
BullCowGame::BullCowGame(int initialBull = 0, int initialCow = 0)
:storedBulls{ initialBull }, storedCows{ initialCow } {}
void BullCowGame::setNumOfCows(string guess, string answer)
{
/*examines each character index location in both strings
to see if there are two characters that are common
in both words, albeit located at different index locations*/
for (unsignedint i = 0; i < guess.size(); i++)
{
if (guess[i] != answer[i])
{
for (unsignedint j = 0; j < guess.size(); j++)
{
if (guess[i] == answer[j])
{
storedCows++;
}
}
}
}
}
int BullCowGame::getNumOfCows()
{
return storedCows;
}
void BullCowGame::setNumOfBulls(string guess, string answer)
{
for (unsignedint i = 0; i < answer.size(); i++)
{
if (guess[i] == answer[i])
{
storedBulls++;
}
}
}
int BullCowGame::getNumOfBulls()
{
return storedBulls;
}
You always need to make sure to return something from a function that has a non-void return type.
In this case it looks like you want to return the return value of askToPlayAgain().
1 2 3 4 5 6
else
{
//if incorrect input is given, ask for answer again
cout << "Please input either 'yes' or 'no'\n" << endl;
return askToPlayAgain();
}
The above code should work (I haven't tested it), but it might not be the "best" solution. In theory you could run out of stack space if the user inputs too many invalid strings. The "solution" would be to implement this in terms of a loop instead of recursion.
Okay. I understand. I was going to use a while loop but having the function recurse just looked cleaner. But the stack space not being freed up would definitely be a problem.
The "solution" would be to implement this in terms of a loop instead of recursion.
Recursion is usually going to be harder to read/write/debug/ modify/ maintain / etc. I avoid it if possible. There are a handful of algorithms where the recursion is the better choice, but those are uncommon in general and rare if not dealing with some specific data structures or sub disciplines.