Error on line EGuessStatus Status = BCGame.CheckGuessValidity(Guess)

Hello,

I have an error on this line in main.cpp file:
EGuessStatus Status = BCGame.CheckGuessValidity(Guess)

and I do not why.

main.cpp

  /* This is the console executable, that makes use of the BullCow class
    This acts as the view on MVC pattern, and is responsible for all
    user interaction. Tod gme logig see the BullCowGame class.
    */

    #include <iostream>
    #include <string>
    #include "FBullCowGame.h"

    using FText = std::string;
    using int32 = int;

    void PrintIntro();
    void PlayGame();
    FText GetGuess();
    bool AskToPlayAgain();


    FBullCowGame BCGame; // instantiate a new game

    // the entry point form the app
    int main()
    {
    	bool bPlayAgain = false;
    	do
    	{
    		PrintIntro();
    		PlayGame();	
    		// TODO: add a game summary
    		bPlayAgain = AskToPlayAgain();
    	} while (bPlayAgain);

    	return 0; // exit the app
    }

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

    	// loop for the number of turns asking for guessing	
    	for (int32 count = 1; count <= MaxTries; count++) // TODO change rfom FOR to WHILE loop once we are validating tries
    	{
    		FText Guess = GetGuess(); //TODO make loop checking valid guess
    		
    		//Submit valid guess to the game
    		FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);

    		EGuessStatus Status = BCGame.CheckGuessValidity(Guess);

    		// print number of bulls and cows
    		//std::cout << "Bulls = " << BullCowCount.Bulls;
    		std::cout << "Bulls = " << BullCowCount.Bulls;
    		std::cout << ". Cows = " << BullCowCount.Cows << std::endl;
    	}
    }

    // introduce the game
    void PrintIntro()
    {	
    	std::cout << "Welcome to Bulls and Cows" << std::endl;
    	std::cout << "Can you guess the " << >BCGame.GetHiddenWordLength();
    	std::cout << " letter isogram I am thinking of?\n";
    	std::cout << std::endl;
    	return;
    }

    FText GetGuess() // TODO Get valid guess
    {	
    	int32 CurrentTry = BCGame.GetCurrentTry();

    	std::cout << "Try " << CurrentTry << ". Enter your guess: ";
    	FText Guess = "";
    	std::getline(std::cin, Guess);

    	// repeat the guess back
    	std::cout << "Your guess was: " << Guess << std::endl;

    	return Guess;
    }

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

    	return (Response[0] == 'y') || (Response [0] == 'Y');
    }

FBullCowGame.h

#pragma once
#include <string>

using FString = std::string;
using int32 = int;

// all values initializing to 0
struct FBullCowCount
{
public:
	int32 Bulls = 0;
	int32 Cows = 0;
};

enum class EGuessStatus
{
	OK,
	Not_Isogram,
	Wrong_Length,
	Not_Lowercase
};


class FBullCowGame
{
public:
	FBullCowGame();

	int32 GetMaxTries() const;
	int32 GetCurrentTry() const;
	int32 GetHiddenWordLength() const;

	bool IsGameWon() const;
	EGuessStatus CheckGuessValidity(FString) const;
	bool CheckGuessValidity(FString); 

	void Reset(); // TODO make a more rich return value. 
	FBullCowCount SubmitGuess(FString);


// Please try and ignore these and focus on the interfase above ^^
private:

	// See constructor for initialisation.
	int32 MyMaxTries;
	int32 MyCurrentTry;
	FString MyHiddenWord;
};

FBullCowGame.cpp

#include "FBullCowGame.h"
using int32 = int;

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

int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; }
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetHiddenWordLength() const { return MyHiddenWord.length(); }


void FBullCowGame::Reset()
{
	constexpr int32 MAX_TRIES = 8;
	const FString HIDDEN_WORD = "planet";

	MyCurrentTry = 1;
	MyMaxTries = MAX_TRIES;
	MyHiddenWord = HIDDEN_WORD;

	return;
}

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

EGuessStatus FBullCowGame::CheckGuessValidity(FString) const
{
	if (false)
	{
		return EGuessStatus::Not_Isogram;
	}
	else if (false)
	{
		return EGuessStatus::Not_Lowercase;
	}
	else if(false)
	{
		return EGuessStatus::Wrong_Length;
	}
	else
	{
		return EGuessStatus::OK;
	}
}


// receives a VALID guess, increments turn, and return valid guess
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
	// increment the turn number
	MyCurrentTry++;

	// stup a return variable
	FBullCowCount BullCowCount;
	

	int32 HiddenWordLength = MyHiddenWord.length();
	// loop throuth all letters in the guess
	for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++)
	{
		// compare letters against the hidden word
		for (int32 GChar = 0; GChar < HiddenWordLength; GChar++)
		{
			if (Guess[GChar] == MyHiddenWord[MHWChar] )
			{
				if (MHWChar == GChar)
				{
					BullCowCount.Bulls++;
				}
				else
				{
					BullCowCount.Cows++;
				}
			}
			// if they match then

		}
	}
			
				// if they are in the same place
					// increment bulls
				// else
					// increment cows if not

	return BullCowCount;
}

I am sorry that i didn’t include the error.

Severity Code Description Project File Line Suppression State
Error C2440 ‘initializing’: cannot convert from ‘bool’ to ‘EGuessStatus’ BullCowGame e:\onedrive\udemy\cppunreal\section_02\bullcowgame\main.cpp 46

What exactly does the error message say? Could you edit your question with that infomation, please?

It’s because you are basically trying to redeclare the same variable over and over again in the for loop.
You have to move the declaration up above the for-loop and inside the for-loop you just assign the new value.

That is actually covered in the videos of Ben. Just rewatch them or watch a little further (don’t know where you are exactly).

EGuessStatus Status; should be above the for loop and
Status = BCGame.CheckGuessValidity(Guess); inside the for loop.

Hi.
Thanks for the help/.

Actually if i declare it before the loop, i have another error :

Severity Code Description Project File Line Suppression State
Error (active) E0513 a value of type “bool” cannot be assigned to an entity of type “EGuessStatus” BullCowGame e:\OneDrive\Udemy\CppUnreal\Section_02\BullCowGame\main.cpp 51

Ok, I was wrong about the redeclaration thing. I don’t quite understand why.

But I found the actual bug:
In your FBullCowGame.h you have this:

 	EGuessStatus CheckGuessValidity(FString) const;
	bool CheckGuessValidity(FString);

You declare the same function with different return values. Just delete the bool one and you should be good to go :slight_smile:

2 Likes

You have implementation of
EGuessStatus CheckGuessValidity(FString) const; in your .cpp file, but you don’t have the implementation of
bool CheckGuessValidity(FString);

1 Like

Thank you for the helpful answers.

Privacy & Terms