Bug: Choosing word length doesn't work for words with over 5 letters

I have tried to make some functions to allow the player to choose the length of word that they would like to play with (choosing between 3 and 7 letters), and it functions fine until the player enters numbers above 5. When they enter 6 or 7, it passes back a word of the last length entered. For example, if I select a 3 letter word on my first play, then, when asked to play again, and I choose 6 letters, it will select the 3 letter word again. If I select 6 or 7 on the first play, it tries to pass back a word of length 0, which is what the WordLength variable is initialised as.

Here are the relevant bits of code:


Lets the player choose the length of the hidden word
void ChooseWordLength() {
	FText Response = "";
	int32 WordLength = 0;
	do {	// TODO Account for double figure numbers
		// Ask the player to choose a hidden word length
		std::cout << "Choose the length of the hidden word, from 3 to 7 letters.\n";
		// Take input from player	
		getline(std::cin, Response);
		WordLength = Response[0] - '0';
	} while (!(WordLength >= 3) || !(WordLength <= 7)); // Repeat the above if the player's input is invalid
	BCGame.SelectHiddenWord(WordLength);	// TODO Fix bug - word lengths above 5 give 5 letter word

	std::cout << "\nCan you guess the " << BCGame.GetHiddenWordLength() << " letter isogram I'm thinking of?" << std::endl;



void FBullCowGame::SelectHiddenWord(int32 WordLength) {
	// Cycle through array of words
	for (int32 Index = 0; Index < HiddenWords->length(); Index++ ) {
		// If the word length matches the input from the player
		if (WordLength == HiddenWords[Index].length()) {
			// Set hidden word
			MyHiddenWord = HiddenWords[Index];

HiddenWords array declaration and initialisation:


	bool IsIsogram(FString Word) const;
	bool IsLowercase(FString Word) const;

	// See constructor for initialisation
	int32 MyCurrentTry;
	FString HiddenWords[5]; // TODO Replace magic number
	FString MyHiddenWord;
	bool bGameIsWon;


void FBullCowGame::Reset() {
	const FString HIDDEN_WORDS[5] = { "ant", "shoe", "eight", "planet", "qualify" }; // TODO Replace magic number // This MUST be an isogram 
	for (int32 Index = 0; Index < HIDDEN_WORDS->length(); Index++) {
		HiddenWords[Index] = HIDDEN_WORDS[Index];
	//MyHiddenWord = HIDDEN_WORDS[0];

	MyCurrentTry = 1;
	bGameIsWon = false;

I know, thanks to debugging, that the correct value for WordLength is being passed into SelectHiddenWord, so I am sure that it must be somewhere in that function that the problem is occurring. Any help would be greatly appreciated.

After a bit of googling, seems like my mistake was in trying to access the length of an array, didn’t realise you can’t do that in C++. For the time being, I have replaced ‘HIDDEN_WORDS->length()’ with 5 (magic number :unamused: If anyone knows how I could avoid using a magic number here, that’s would be helpful :slight_smile:

Privacy & Terms