My constructor is being ignored

Whenever I try to implement the constructor, copy and pasting the private class information over into the constructor like in the video it ignores the constructor values. Instead going straight into the header file private for the variables.

When I remove the numbers from the private section of the header file, it reads as “Try 0” and ignores the loops in main.cpp entirely. The only way the code runs is if there’s a vlaue in the private section of the header file.

FBullCowGame.h

#pragma once
#include

class FBullCowGame {
public:
FBullCowGame(); //Constructor

void Reset(); //TODO make a more rich return value
int GetMaxTries() const;
bool IsGameWon() const;
int DisplayCurrentTry() const;
void CheckAnswer(std::string);

private:
int MyCurrentTry;
int MyMaxTries;
};

FBullCowGame.cpp

#include “FBullCowGame.h”

FBullCowGame::FBullCowGame()
{
int MyCurrentTry = 5;
int MyMaxTries = 1;
}

void FBullCowGame::Reset(){ return; }

int FBullCowGame::GetMaxTries() const { return MyMaxTries; }

bool FBullCowGame::IsGameWon() const { return false; }

int FBullCowGame::DisplayCurrentTry() const { return MyCurrentTry; }

void FBullCowGame::CheckAnswer(std::string){ }

main.cpp

#include
#include
#include “FBullCowGame.h”

void PrintIntro();
std::string GetGuess();
bool AskToPlayAgain();
FBullCowGame BCGame;
void PlayGame();

void PlayGame()
{

int MaxTries = BCGame.GetMaxTries();
// loop for the number of turns asking for guesses
std::cout << MaxTries << std::endl;
for (int count = 1; count <= MaxTries; count++)
{
std::string Guess = GetGuess();
std::cout << “You guessed: \n” << Guess;
}
}

void PrintIntro()
{
// introduce the game
constexpr int WORLD_LENGTH = 5;
std::cout << “Welcome to Bulls and Cows, a cool and good word game.\n” << std::endl;
std::cout << "Can you guess the " << WORLD_LENGTH;
std::cout << " letter isogram I’m thinking of? ";
return;
}
std::string GetGuess()
{
int CurrentTry = BCGame.DisplayCurrentTry();
std::cout << "Try " << CurrentTry << std::endl;
// get a guess from the player
std::string Guess = " ";
std::cout << "\nYour guess: ";
std::getline(std::cin, Guess);
return Guess;
}

bool AskToPlayAgain()
{
std::cout << "Do you want to play again? (y/n) ";
std::string Response = “”;
std::getline(std::cin, Response);

(Response[0] == ‘y’);
std::cout << std::endl;
return (Response[0] == ‘y’) || (Response[0] == ‘Y’);
}

// The intro point of our program
int main()
{
std::cout << BCGame.DisplayCurrentTry();
bool bPlayAgain = false;
do {
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
} while (bPlayAgain);
return 0; //exits application
}

Hey there,

Try removing the int before the variable names inside the constructor only. I’m on the road but will reply with a lengthier post later as to why, and will also provide you with some better sintax for initializing member variables in C++.

Hope it helps!

5 Likes

I figured it out! Thank you so, so much! It turns out the int was throwing it off like you mentioned.

You are a life saver, I have been stuck on that for a while. If you could elaborate as to why that was causing a problem I would greatly appreciate it.

Hey,

By using the syntax in the OP constructor (int variableName), you are actually defining a local variable inside the constructor, instead of assigning the value to the member variable.

In this particular case, a local variable was being defined and initialized. Then as soon as the constructor ends, its local variables go out of scope. The member variables never got the desired values assigned.

By removing int from before the variable name, you are assigning the values to the member variables, which retain their value until changed, and for the lifetime of the object.

5 Likes

The int in the constructor got me too, thanks for posting the question.

I know it’s 7 months later, but thanks. I hadn’t spotted that!

Thank you!

Privacy & Terms