Hi!
I started getting this error during this section, have gone back over the code multiple time and finally just decided to download the project from Git. Still getting the same error on the Git version. Does anyone know what could be causing this? Have also looked it up on stack overflow but the answers are over my head as I am a total noob.
could you please also post your source code? The error you are getting might be because of a loop that goes bad at some point, but without the source ode it is impossible to figure out what may cause it.
When pasting code, use indent preformatted text by 4 spaces formatting or pastebin.com, as it will help a lot when it comes to code readability
Ben will go over this in a future lecture but it’s happening because you are going out of bounds of the string. Consider the following code:
std::string word = "Hi";
for(int i = 0; i < 3; ++i)
{
std::cout << word[i];
}
When i = 0 “H” will be printed to the console, then “i” for 1, and then when i = 2 you would be out of range when trying to use the subscript operator [] on word, which is what that error message is telling you.
Thanks everyone,
I’m actually starting at the beginning and retaking the course, a lot of the concepts did not sink in the first time.
DanM I looked back through my project and this was the answer, thank you.
Hi,
I went back through the project and getting the same error. tried using pastebin to show my code here but am getting the error " New users only allowed 2 links per post. Only using the pastebin link in my post so not sure how to get around this.
Hi @Jeremy_Thompson,
Quick way to get around it is to paste your code directly in the editor here on Gamedev. Then select the text, and click the </> button (tooltip: Preformatted text). That should work just fine.
#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 i = 0; i < HiddenWordLength; i++) {
// compare letters against the hidden word
for (int32 j = 0; j < HiddenWordLength; j++) {
// if they match then
if (Guess[i] == MyHiddenWord[i]) {
if ( i == j) {
BullCowCount.Bulls++; //incerment bulls if not
}
else {
BullCowCount.Cows++; // else increment cows
}
}
}
}
return BullCowCount;
}
OK! thank you. So I am getting the same “Expression: String Subscript out of Range” Error but only if I type a one character guess. Any guess with more than one character returns bulls and cows.
Any ideas on how to fix this??
Apparently, I’ve posted similiar issue as out of bounds error when running the console. Just to let you know, none of the above solutions work for me. It keeps giving error on a specific line of code (42 in my case) which looks like:
Do you know which video he explains this in? Is it in section 2 cuz i finished section 2 and didn’t see the soIution. I don’t want to start section 3 without having it all working unless that’s where he explains it.
“Writing Error Checking Code” as we check to make sure the guess is valid before going to that function. The problem as described in the OP happens because the guess is shorter than the hidden word so the for loop goes out of bounds
That’s not the error I’m getting. It works when I put in less letters or more letters or uppercase or anything with the enum besides when I get 5 letters. So if the hidden word is 5 letters and I put in 5 letters (regardless of how many bulls or cows) then I get the error. I posted my source code here - Debug Assertion Failed . Any idea what I did wrong?
Ive been continually getting this error and it seems nothing i try works. I know it has to do with the subscripts being out of range i just cant figure out why.
its only an issue if i type in a single character. if i type in 2 it works. im assuming its because [i] == [0], not sure how 0 can be out of range for an index. could [i] == [-1] ???
I must say this is not quite the correct solution. This code only works when the hidden word and the guessed word have the same length, otherwise it may or may not cause an issue. e.g. it will cause an assertion failure when your hidden word has 5 letters and you try a guess with a 3-letter word.
The problem is this code assumes that Guess.length() == MyHiddenWord.length() always! So instead of using MyHiddenWordLength in both for loops, you use Guess.length() in the first loop, and as a result use the appropriate index (j) for the MyHiddenWord. See this corrected version:
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
//increment turn number
MyCurrentTry++;
//setup a return variable
FBullCowCount BullCowCount;
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 i = 0; i < Guess.length(); i++)
{
for (int32 j = 0; j <= HiddenWordLength - 1; j++)
{
if (TheGuess[i] == MyHiddenWord[j])
{
if (i == j)
{
BULL_COW_COUNT.Bulls++;
}
else
{
BULL_COW_COUNT.Cows++;
}
}
}
return BULL_COW_COUNT;
}
}