I'm having an error on Visual Studio and I need some help

So I already posted my problem in the udemy Q&A. Here is the question. Thanks for taking your time to look at it. Not sure if this is a problem with my PC or Visual Studio. Anyways if anyone has anything they can say to help please do as this problem is holding me back in my learning.

Thanks a lot,
Berdj

Could you paste up a copy of the whole code rather than just the screen shots of part of the file please @Berdj_Stepanian.

@Rob

Main.cpp

/* This is the console executable that makes use of the BullCow class
This acts as the view in a MVC pattern, and is responsible for all 
user interaction. For game logic see the FBullCowGame 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 for our application
int main()
{
	bool bPlayAgain = false;
	do {
		PrintIntro();
		PlayGame();
		bPlayAgain = AskToPlayAgain();
	}
	while (bPlayAgain);
		return 0; // exit the application
	}

	// introduce the game
void PrintIntro() 
{
	constexpr int32 WORD_LENGTH = 9;
		std::cout << "Welcome to Bulls and Cows, a fun word game.\n";
		std::cout << "Can you guess the " << WORD_LENGTH;
		std::cout << " letter isogram I'm thinking of?\n";
		std::cout << std::endl;
	return;
}


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

	// loop for the number of turns asking for guesses
	// TODO change from FOR to WHILE loop once we are validating tries
	for (int32 count = 1; count <= MaxTries; count++) {
		Ftext Guess = GetGuess(); //TODO make loop check for valid guesses

	// submit valid guess to the game, and receieve counts
		FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);
	// print # of bulls and cows
		std::cout << "Bulls = " << BullCowCount.Bulls;
		std::cout << ". Cows = " << BullCowCount.Cows << std::endl;
	}
	//TODO summarize game
}


	// get a guess from the player
Ftext GetGuess() 
{
	int32 CurrentTry = BCGame.GetCurrentTry();

	std::cout << "Try " << CurrentTry << ". Enter your guess: ";
	Ftext Guess = "";
	std::getline(std::cin, Guess);
	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');

	return false;
}

FBullCowGame.h

#pragma once
#include <string>

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

// all values initialized to 0
struct FBullCowCount
{
	int32 Bulls = 0;
	int32 Cows = 0;
};

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

	int32 GetMaxTries() const;
	int32 GetCurrentTry () const;
	bool IsGameWon() const;

	void Reset(); // TODO make a more rich return value.
	bool CheckGuessValidity(FString);
	FBullCowCount SubmitGuess(FString); // counts bulls & cows, and increases try # assuming valid guess
	

// ^^ ignore Private class right now. focus on interface above ^^
private:
	// See constructor for intialization
	int32 MyCurrentTry;
	int32 MyMaxTries;
	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; }

void FBullCowGame::Reset()
{
	constexpr int32 MAX_TRIES = 8;
	MyMaxTries = MAX_TRIES;

	const FString HIDDEN_WORD = "ant";
	MyHiddenWord = HIDDEN_WORD;

	MyCurrentTry = 1;
	return;
}

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

bool FBullCowGame::CheckGuessValidity(FString)
{
	return false;
}

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

	// setup a return variable
	FBullCowCount BullCowCount;

	// loop through all letters in the guess
	int32 HiddenWordLength = MyHiddenWord.length();
		for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++) {
			// compare letters against the hidden word
				for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
					// if they match then
							if (Guess[GChar] == MyHiddenWord[MHWChar]) {
								if (MHWChar == GChar) { // if they're in the same place
									BullCowCount.Bulls++; // incriment bulls
								}
								else {
					BullCowCount.Cows++; // must be a cow
				}
			}
		}
	}
	return BullCowCount;
}

Does this help?

Looks like the same issue and you have both got shorter words so the explanation given seems to make sense.

1 Like

My understanding so far is that the error is caused if someone enters less characters than the amount of characters in the hiddenword. Unfortunately I can’t really test if this is the problem or not because I’m experiencing another problem at the moment but I’ll let you know as soon as possible. Thanks for the help so far.

1 Like

Privacy & Terms