My try counter says 0 when it starts why?

My main.cpp
#include
#include
#include “FBullCowGame.h”

void PrintIntro();
void PlayGame();
std::string GetGuess();
bool AskToPlayAgain();
FBullCowGame BCGame; // instance of new game

int main() {

bool bPlayAgain = false;
do {
	PrintIntro();
	PlayGame();
	bPlayAgain = AskToPlayAgain();
} while (bPlayAgain);
return 0; //exit the application

}
// Introduce the game
void PrintIntro() {

constexpr int WORD_LENGTH = 5;
std::cout << "Welcome to Bulls and Cows, a fun word game\n";
std::cout << "Can youu guess the " << WORD_LENGTH << " letter isogram I'm thinking of?\n";
std::cout << std::endl;
return;

}

void PlayGame() {
BCGame.Reset();
int MaxTries = BCGame.GetMaxTries();

//Loop for number of guess'
//TODO change from FOR to WHILE loop once we are validating tries
for (int count = 1; count <= MaxTries; count++) {
	std::string Guess = GetGuess(); // TODO make loop checking valid
	//submit valid guess to the game
	//print number of bulls and cows


	// Repeat the guess back to the player
	std::cout << "You entered: " << Guess << std::endl;
	std::cout << std::endl;
}

// TODO summarise game

}

// Get a guess from a player
std::string GetGuess() {
int CurrentTry = BCGame.GetCurrentTry();
std::string Guess = “”;
std::cout << “Try " << CurrentTry << " What is your guess?”;
std::getline(std::cin, Guess);

return Guess;

}

bool AskToPlayAgain(){
std::cout << “Do you wnat to play again (y/n)?”;
std::string Response = “”;
std::getline(std::cin, Response);
return (Response[0] == ‘y’) || (Response[0] == ‘Y’);
}

My FBullCowGame.h
#pragma once
#include

class FBullCowGame {
public:
FBullCowGame(); //Constructor
int GetMaxTries() const;
int GetCurrentTry() const;
bool IsGameWon() const;

void Reset();
bool CheckGuessValidity(std::string);
// provide a method for counting bulls and cows and incrementing try #

private:
// see constructor for initialisation
int MyCurrentTry;
int MyMaxTries;

};

my FBNullCowH=Game.cpp
#include “FBullCowGame.h”

FBullCowGame::FBullCowGame()
{
Reset();
}

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

void FBullCowGame::Reset()
{
constexpr int MAX_TRIES = 5;
MyMaxTries = MAX_TRIES;
int MyCurrentTry = 1;

return;

}

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

bool FBullCowGame::CheckGuessValidity(std::string)
{
return false;
}

in your Reset method you have this *int* MyCurrentTry = 1; that means you’re creating a local (to the function) variable called MyCurrentTry, and aren’t setting the class’ variable MyCurrentTry to 1. Remove the int on that line and it should work.

1 Like

thanks a lot. Removed the try and voilà it worked.
cheers

1 Like

Thanks too! I did the same error :smiley:

Great advice!

Thanks!:slight_smile:

Privacy & Terms