Alternative method but less effective?

I came up with another solution to this challenge, which went through making another function to print the guess. The problem I have with this is that every variation of this solution I come up with carries some problems. My base solution was this:

#include <iostream>
#include <string>

using namespace std;

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

void PrintGuess(string Guess);

int main() {

	PrintIntro();

	PlayGame();

	return 0;
}

void PlayGame()
{
	//Loop for number of turns
	constexpr int TURNS = 5;
	for (int i = 0; i < TURNS; i++) {	
		PrintGuess(GetGuess());
	}
}

void PrintIntro() {
	constexpr int WORD_LENGTH = 5;
	cout << "Welcome to Bulls and Cows game" << endl;
	cout << "let's see if you can guess my " << WORD_LENGTH << " letter isogram\n";
	return;
}

string GetGuess() {
	string Guess = "";
	//Get guess form player
	cout << "Enter your guess: ";
	getline(cin, Guess);
	return Guess;
}

void PrintGuess(string Guess)
{
	//Print the player's guess
	cout << "Your guess was: " << Guess;
	cout << endl << endl;
	return;
}

However, in this I see a problem if you need the returned value (the guess) for another function/action, since with this method, that returned value is lost.

An alternative to this is to store the value in the variable “Guess” and then call it with as many functions as you need, something like this:

void PlayGame()
{
	//Loop for number of turns
	constexpr int TURNS = 5;
	for (int i = 0; i < TURNS; i++) {	
		string Guess = GetGuess();
		PrintGuess(Guess);
	}
}

But for this, you need to declare the variable “Guess” either in PlayGame() or as a global variable. These two options both carry problems: declaring it in PlayGame() makes it inaccesible for the function GetGuess(), while defining it as global may grant unwanted access from other functions.

If it is declared in PlayGame(), a second variable (for example “string Guess2”) could be declared in GetGuess() to allow for functioning, but then you’re increasing the memory used by the program as you’re declaring and using more variables.

The final solution I though is doing a double declaration of the variable “Guess”, but then, the previous value of the variable “Guess” would be lost. While this is not a problem in this case, in the case, for example, of concatenating strings, it would become a problem.

Any input on what the best path would be?

If you have reached here, thanks.

I think you’re probably overthinking things too much. You’re not really going to have to worry about the amount of memory that’s being allocated in these situations, because the variables only exist within the scope of the block they’re created in.
For example, in your PlayGame() function, every time you iterate through the ‘for’ loop a string variable is being created, but it only lives until after PrintGuess() finishes. When it’s value is passed to PrintGuess(), the variable that stores it is a new variable that only lives for the duration of the PrintGuess() function. As soon as it returns, it is gone.
All these local variables are pushed onto something called the stack, and then popped off the stack when they are no longer required (like when a function returns, or when the you reach the end of a block of code).
The only time you really have to worry about how you’re handling memory is when you’re allocating it yourself on the heap, with commands like malloc. You’ll probably want to think twice before using it inside a ‘while’ loop! But as long as that memory is freed before the end of every iteration even that shouldn’t be an issue.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms