Lecture 21 using functions alternative program

As a matter of information, when I press control F5 I need to compile any changes I make prior to execution which does not seem to be in the video.
In regard to the code, I wrote a second function using void GetGuess rather than using string. The program executed well. Is this incorrect or just less elegant?
Here is the code:
#include
#include

using namespace std;

void PrintIntro();

void GetGuess();

// the entryy point for our application
int main()
{
PrintIntro();

GetGuess();

}

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

void GetGuess() {
// get a guess from the player
string Guess = “”;
cout << "Enter your guess? ";
getline(cin, Guess);
// repeat the guess back to them
cout << "Your guess was: " << Guess << endl;
cout << endl;

// get a guess from the player	
cout << "Enter your guess? ";
getline(cin, Guess);
// repeat the guess back to them
cout << "Your guess was: " << Guess << endl;
cout << endl;
return;

}

Thanks

There’s nothing wrong with it for now, and since you aren’t using the guess anywhere outside the function making it void actually makes more sense. However, the instructor was preparing you for later videos, where you’ll need to return the guess and use it in functions outside of GetGuess(), so keep that in mind when you go forward, you will have to change it eventually. Also, when you post code on here, highlight it and hit the “Preformatted text” button, makes it a lot easier to read.

1 Like

Thank you for your response.
I am having to copy the code to a word document and then copy it to the
discussion forum which is why it is hard to read.
Thanks again

Privacy & Terms