Returning from a method is adding a blank "guess" to input stream

Howdy,
I’m looking for feedback on what I might be doing wrong in my approach to the following problem.

I am currently at S02 L53, doing the optional challenge since I already have the functionality of the base challenge functional. However, I’m getting an extra newline in the input stream buffer when returning from a method. Here’s a screenshot of the issue at hand.

Here’s the code snippet I have in the general area of the problem:

//int main() is above this excerpt in my .cpp file
///////////////////////

void PlayGame()
{
	BCGame.Reset();

	BCGame.SelectWordByLength(AskForWordLength()); //AskForWordLength() is a method that prompts the user to define how many letters they want in the isogram. It returns an int32.  does what Lecture 53 asks, get the number of tries available as defined by the word length.
//SelectWordByLength() takes an int32 as an parameter, and selects the word from the method at the bottom of this excerpt, then assigns it to the "HiddenWord" private member variable of the BCGame class.

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

// the PlayGame() method contains the while loop after this, then ends.

// Here is the method for getting the user's preferred word length. It is properly prototyped before main():
int32 AskForWordLength()
{
	int32 WordLengthChoice = 0;
	std::cout << "How long (in letters) should the word be (3 to 6)?";
	std::cin >> WordLengthChoice; //error prone but simple for the sake of getting it functional
	return WordLengthChoice;
}

// Here is the method for selecting the word length. It's a class method defined in the FBullCowGame.cpp file.
void FBullCowGame::SelectWordByLength(int32 PlayerChoice) //PlayerChoice is the int32 returned by AskForWordLength().
{
	TMap <int32, FSTRING> WordLenghGetsWord{ { 3, "hat"}, { 4, "plum" }, { 5, "spain" }, {6, "danger"} };
	MyHiddenWord = WordLenghGetsWord[PlayerChoice];
	return; // I have tried omitting this to see if it resolves the problem, it doesn't.
}

My goal is to omit the unnecessary line break that is causing an invalid “guess”.

Thanks!
Randall

Here is the git link to my complete source. https://github.com/classicstyleatx/bullcowgame

Hello @Randall_L,

replace

int32 AskForWordLength()
{
	int32 WordLengthChoice = 0;
	std::cout << "How long (in letters) should the word be (3 to 6)?";
    std::cin >> WordLengthChoice; 
	return WordLengthChoice;
}

with

int32 AskForWordLength()
{
	int32 WordLengthChoice = 0;
    std::cout << "How long (in letters) should the word be (3 to 6)?";
    std::getline(std::cin, WordLengthChoice);
	return WordLengthChoice;
}

Does it work now as you expected?

Cheers
Kevin

Privacy & Terms