Declaring a const instead of running from playgame function?

Here is My Code:

#include <iostream>
#include <string>

using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
const string &PrintGuess(string &Guess);


// entry point into our application
int main()
{
	PrintIntro();
	PlayGame();
	return 0;
}



void PlayGame()
{
	//loop number of turns asking for players guesses
	constexpr int NUMBER_OF_TURNS = 5;
	for (int count = 1; count <= NUMBER_OF_TURNS; count = count + 1)
	{
		GetGuess();
		const string &PrintGuess(string &Guess);
		cout << endl;
	}
}



// Game Intro
void PrintIntro() 
{
	// introduce the game

	constexpr int WORD_LENGTH = 5;

	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";
	return;
}



// Gets The Player's Guess
string GetGuess() 
{
	
	string Guess = "";
	// get a guess from the player
	cout << endl << "Enter your guess: ";
	getline(cin, Guess);
	cout << endl;
	
	return PrintGuess(Guess);
}


//Prints the Player's Guess
const string &PrintGuess(string &Guess)
{
	// repeat the guess back to them
	cout << "Your Guess was: " << Guess << endl;
	return Guess;
}

The question is would this be acceptable as it does work as intended or is the instructors solution a better way for this?

Help me a little bit, which lecture is this from? I’m further ahead so I’m having to think back and try to remember whether this is later addressed or not.

I’m not sure what you’re intending to do with this or what your question is. Though

for (int count = 1; count <= NUMBER_OF_TURNS; count = count + 1)
{
	GetGuess();
	const string &PrintGuess(string &Guess);
	cout << endl;
}

The second line in this loop is just redeclaring the function and isn’t needed. It’ll work if you remove this line.

Also I’m curious as to why you’ve used references for your print guess function. Was it automatic generated by visual studio?

This code is for Section 2 Lecture 22 Clarity is worth fighting for.

@DanM Actually if the second line is removed it doesnt print the guess for me and yes VS2017 auto made the function const. As for the question is in reference to separating the GetGuess/PrintGuess Function and implementing the PrintGuess function as a new function. Although as stated in the OP it works as intended.

"Actually if the second line is removed it doesnt print the guess for me"
I said second line in the loop so

const string& PrintGuess(string& Guess);

"VS2017 auto made the function const"
This function isn’t const, the return type is. The difference is that a const function - which can only be applied to a member function - means that the function doesn’t modify any class data. On the other hand a const return type means the value that is returned is const.

I’m asking if it was VS that added references (&).

You aren’t seperating GetGuess and PrintGuess completely as GetGuess is calling PrintGuess. Your code simplified looks like this

#include <iostream>
#include <string>
using namespace std;
void PrintIntro();
void PlayGame();
string GetGuess();
const string PrintGuess(string Guess);
// entry point into our application
int main()
{
	PrintIntro();
	PlayGame();
	return 0;
}
void PlayGame()
{
	//loop number of turns asking for players guesses
	constexpr int NUMBER_OF_TURNS = 5;
	for (int count = 1; count <= NUMBER_OF_TURNS; count = count + 1)
	{
		GetGuess();
		cout << endl;
	}
}
// Game Intro
void PrintIntro()
{
	// introduce the game
	constexpr int WORD_LENGTH = 5;

	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";
	return;
}
// Gets The Player's Guess
string GetGuess()
{
	string Guess = "";
	// get a guess from the player
	cout << endl << "Enter your guess: ";
	getline(cin, Guess);
	cout << endl;
	return PrintGuess(Guess);
}
//Prints the Player's Guess
const string PrintGuess(string Guess)
{
	// repeat the guess back to them
	cout << "Your Guess was: " << Guess << endl;
	return Guess;
}

Though that has the side effect of you passing in Guess to PrintGuess just to return the value back. So it seems somewhat unecessary. So cleaning that up the code becomes

#include <iostream>
#include <string>
using namespace std;
void PrintIntro();
void PlayGame();
string GetGuess();
void PrintGuess(string Guess);
// entry point into our application
int main()
{
	PrintIntro();
	PlayGame();
	return 0;
}
void PlayGame()
{
	//loop number of turns asking for players guesses
	constexpr int NUMBER_OF_TURNS = 5;
	for (int count = 1; count <= NUMBER_OF_TURNS; count = count + 1)
	{
		string Guess = GetGuess();
		PrintGuess(Guess);
		cout << endl;
	}
}
// Game Intro
void PrintIntro()
{
	// introduce the game
	constexpr int WORD_LENGTH = 5;

	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";
	return;
}
// Gets The Player's Guess
string GetGuess()
{
	string Guess = "";
	// get a guess from the player
	cout << endl << "Enter your guess: ";
	getline(cin, Guess);
	cout << endl;
	return Guess;
}
//Prints the Player's Guess
void PrintGuess(string Guess)
{
	// repeat the guess back to them
	cout << "Your Guess was: " << Guess << endl;
}

Yes VS2017 did add those automatically

As for the way it was done… if its unnecessary I will revert the code to the original solution as presented by the instructor… Thank you for your help and time… I really do appreciate it. Also, thank you for the explanation of the difference of the Function and The Return Type. I am still trying to remember alot of what was forgotten 15+ years ago :blush:

Privacy & Terms