GetGuess()

Ok so, I kind of took quite literally that GetGuess MUST return a string. I know from #C that you can add function to output so I try the same and it work but I wanted to be sure that if I do it that way, I won’t have problems later on.
(I’m using namespace std)

void printIntro();
string getGuess();

int main()
{
printIntro();
cout <<"Your guess was "<<getGuess()<<endl;
cout << "Your guess was " << getGuess() << endl;
return 0;
}

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

string getGuess()
{
string guess = “”;
cout << "Enter your guess ";
getline(cin, guess);
return guess;
}

Generally whenever you realize you’re typing in the same code multiple times, that’s a good
sign that you need to put that code in a new function. Then you can call that function multiple times from
another function without duplicating all the code.

1 Like

Did challenge string GetGuess() prior to moving forward :slight_smile:

Privacy & Terms