My bad code for this section that arguably works

Seems this might need an update as newer versions of Visual Studio 2017 do add prototypes instead of creating header files.

So I tried to do something similar to the code Ben eventually shows us to, but I think my previous small amount of time with .net has confused me. I attempted to define the Guess variable from the GetGuess function by typing ‘GetGuess(Guess)’ where I wanted to call that variable. Which obviously didn’t work.

Eventually I settled on the bad solution of making Guess a global variable. The code works, but only so long as we don’t need to do anything more complicated with it than recalling what it is at that given moment.

#include
#include

using namespace std;

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

string Guess;

// entry point of program
int main()
{

PrintIntro();
PlayGame();

return 0;
}

void PrintIntro()
{
// introduce bulls vs bums or whatever
constexpr int WORD_LENGTH = 5;

cout << “Welcome to Bills and Sows, a lovely game for nice people” << endl;
cout << “Can you work out what " << WORD_LENGTH;
cout << " letter isogram I’m thinking of?” << endl;
cout << endl;
return;
}

void PlayGame()
{
// loop the questions and answers
constexpr int TurnLimit = 5;
for (int Count = 1; Count <= TurnLimit; Count++)
{
GetGuess();
PrintGuess();
}
}

string GetGuess()
{
// get input of guesses from player
string Guess = “”;
cout << “Enter your guess now:” << endl;
getline(cin, Guess);
return Guess;
}

string PrintGuess()
{
// return player’s guess
cout << "You guessed: " << Guess << endl;
cout << endl;

return Guess;
}

I’m on lecture 42 of this section and Ben hasn’t yet covered passing by value vs. reference. All of the arguments he has used so far have been passed by value. This means that the program makes a copy of the value you pass, stores that elsewhere in memory, and then gets rid of it once the function is taken off the stack. However, instead you can directly reference a variable, have a function change it, and it will then remain in memory after the function pops. examples of how your code could be changed to do what you want without the global:
1:
string GetGuess(); //prototype
void PlayGame(){
string Guess = “”;
//etc,etc
Guess = GetGuess();
//etc, etc
}
you’re scoping guess to both functions. It’s important to remember that when you call function, it goes onto the stack, does its thing, then pops off the stack and everything inside the function is gone. Any variables you declared or initialized inside that disappear once the function finishes. So, if you declare guess in one function, and then in another, their scope is separate. That is unless the declaration occurs at a higher level, or you’re passing by reference. Here’s how I did it passing by reference:

void getGuess(string&); //prototype. & denotes that the string is passed by reference
//doesnt return anything because it changes the value passed into it.
void playGame(){
string guess = “”;
getGuess(guess);
//etc,etc
}
void getGuess(string &guess){
//same code you used, but declaration and initialization of guess is unnecessary.
//also, no return statement
//instead, it gets passed into the function, is changed by the function, remains in memory
//because it is scoped to playGame(), and then getGuess() pops off the stack.
}

Privacy & Terms