Need help to remove a bug

Hello guys,

So I just finished my Bulls and Cows game, I even added difficulty settings and 10 difficult random words for each difficulty setting. Its almost complete except for this annoying bug which I cant seem to remove. I dont know how to explain it but I will try my best.

So when I start my game and choose the difficulty setting and as soon as I press enter, the game automatically takes a Guess input even though I dont input anything. I think it takes in a blank space ("") and returns the sentence “Please enter a 3 letter word”.

Here is a screenshot:

Here’s the github link to my code:

Please help me to fix this and also when you do let me know what was causing it.

Thanks a bunch :grin:

P.S. While your at it your more than welcome to go ahead and give my game a try, its a bit challenging :wink:. Don’t forget to tell me how the game is though :+1:.

SOLUTION: Used std::getline instead of std::cin

Your problem is with this line of code std::cin >> DifficultyInput;. When you enter that number, say 1 and then press enter, the input buffer will be “1\n”, the 1 will go into DifficultyInput and then you will be left with “\n” in the buffer which would be used the next time you use std::cin, to solve this you will have to throw away the buffer, or you could use std::getlline and use std::stoi to get that as an int. Solution for the first would be

//clear error flags
std::cin.clear();
//reads max streamsize upto and including a newline and discards it
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Also is there any particular reason you did this

TSet<char> Difficulties = { '1', '2', '3', '4', '5' };
bool bLoop = false; 
while (!bLoop)
{
    //code
}

As opposed to just a simple conditional?

while (DifficultyInput < 1 || DifficultyInput > 5)
{
    //code
}
1 Like

Thanks for your reply Dan. I tried your first solution but when I did that the program would not continue after the difficulty input. It would stop after it took the input and crash if I pressed enter twice.

What I did was paste the first solution code before the std::cin >> DifficultyInput
I also tried this:

std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') >> DifficultyInput;

Both gave same results mentioned above.

However when I tried your getline solution, it worked perfectly and I managed to correct the bug.

Regarding my loop condition, the reason I did that was because I wanted to catch any type of user input. I did not want the code to crash if the user entered any other character other than a number. Another reason is that Im new to c++. So I wanted to learn more and get familiar with different data types in c++. I was intrigued when Ben mentioned TSet in a lecture, so out of that curiosity I wrote such a code.

Anyways thanks a bunch for your reply, it helped. Even if the first solution did not help I learned something new. Cheers :grin:

That would be your problem. I guess I should have made it clear that you do that after taking an input

std::cin >> DifficultyInput;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//continue

Since you’re still getting the input in the exact same method and doing pretty much the exact same thing, it wouldn’t make a difference. Though I can attest to using something just because it’s new and interesting, once I got onto learning about function templates I used it for making a function that only really took two different types and it would probably have made more sense to just have an overloaded function for the other type

Privacy & Terms