Sorta Stolen solution

I couldn’t figure it out on my own. After looking at HavocBlast’s code, it finally made sense. I kept trying to run through Google and cplusplus.com to figure it out and the examples given to my (probably poorly worded questions) were more confusing than helpful. After looking at HavocBlast’s code it started to add up.

  1. I just needed to prototype PrintGuess with a string parameter.
  2. I needed to give a parameter name that I would then push through “cout”
  3. By including the PrintGuess function into my GetGuess function, I just had to add “Guess” and it would print properly.

The only problem I see after writing this out is that it didn’t seem to fully separate the function. I’m still printing the guess from within my GetGuess function. Although, the concept of passing variables is starting to make sense. Thanks HavocBlast!

#include
#include
using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
void PrintGuess(string);

//the entry point for the application
int main()
{
PrintIntro();
PlayGame();
return 0;
}

void PlayGame()
{
//loop for number of turns asking for guess
constexpr int NUMBER_OF_TURNS = 5;
for (int i = 0; i < NUMBER_OF_TURNS; i++)
{
GetGuess();
cout << endl;
}
}

//introduce the game
void PrintIntro()
{
constexpr int WORD_LENGTH = 5;
cout << “Welcome to Bulls and Cows, a fun word game” << endl;
cout << “Can you guess the " << WORD_LENGTH << " letter isogram I’m thinking of?\n”;
cout << endl;
return;
}

//get a guess from the player and print it back
string GetGuess()
{
string Guess = “”;
cout << "What word am I thinking? ";
getline(cin, Guess);
PrintGuess(Guess);
return Guess;
}

void PrintGuess(string Guess)
{
cout << "You Guessed: " << Guess << endl;
return;
}

After watching the rest of the video, I figured out how to make use of the instructor’s solution, as well as the passing function.

I didn’t realize that by declaring a variable with the value of a function (basically the return value), that the function would run in order to return the value to be assigned.

Basically, I didn’t know that “string Guess = GetGuess();” would run the function in order to assign the value. My program would ask me twice for my guess.

Since I was assigning the return value of “GetGuess()” in the “PlayGame” function, I was then able to pass that value through my “PrintGuess” function without it being inside my “GetGuess” function.

Sorry for the lengthy reply. I thought it would be helpful to share what I learned.

Here is my revised code,

Privacy & Terms