My splitting of get and print

[code]#include
#include

using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
string PrintBack();
string Guess = “”;

int main()
{
PrintIntro();
PlayGame();

return 0; // exit the application

}

void PlayGame()
{
// loop for the number of turns, asking for guesses
constexpr int NUMBER_OF_TURNS = 5;
for (int count = 1; count <= NUMBER_OF_TURNS; count++)
{
GetGuess();
PrintBack();
cout << endl;
}
}

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

// get a guess from the player
string GetGuess()
{
cout << "What is your guess: "; getline(cin, Guess);
return Guess;
}

// print the guess back to them
string PrintBack()
{
cout << "You wrote: " << Guess << endl;
return Guess;
}[/code]

Creating global variables (in this case Guess) is generally a bad idea and you should have a good reason to use them (like the FBullCowGame class that is created in this section). Otherwise you should normally pass things around by functions. So your code becomes

#include <iostream>
#include <string>
using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
void PrintBack(string Guess);

int main()
{
    PrintIntro();
    PlayGame();
    return 0; // exit the application
}
void PlayGame()
{
    // loop for the number of turns, asking for guesses
    constexpr int  NUMBER_OF_TURNS = 5;
    for (int count = 1; count <= NUMBER_OF_TURNS; count++)
    {
        string Guess = GetGuess();
	PrintBack(Guess);
	cout << endl;
    }
}
// introduce the game
void PrintIntro()
{
    constexpr int  WORD_LENGTH = 9;
    cout << "Welcome to Bulls and Cows, a fun word game!\n";
    cout << "Can you guess the " << WORD_LENGTH;
    cout << " letter isogram I'm thinking of?\n";
    cout << endl;
    return;
}
// get a guess from the player
string GetGuess()
{
    string Guess = "";
    cout << "What is your guess: ";
    getline(cin, Guess);
    return Guess;
}
// print the guess back to them
void PrintBack(string Guess)
{
    cout << "You wrote: " << Guess << endl;
}

Thanks! I soon realized this upon watching the next part of the video. :slight_smile:

Privacy & Terms