I am a beginner hobbyist and have recently completed a basic Hangman program. I have main loop that gets a letter guess input from the user uses it to update the word and whatnot, then restarts at the beginning if the full word hasn't been guessed or the player hasn't used up all their allotted mistakes.
Anyway, I have the input going into a char variable, and I noticed when I tested putting multiple letters into it at once (to see if it might cause an error), it seems to create a 'queue' of inputs, would accept the first into the char variable, run the loop, then when the loop reset it would accept the next letter in the queue automatically at the time for input.
I was mainly curious what sort of process the language and computer is going through during this.
Sounds like you need cin.ignore(1000, ā\nā); after the input statement. Best thing is to show us the code, at least the input loop and variable declarations.
Thanks for the response, I'm not really interested in fixing it or preventing it atm, more just wanting to know what the process is / how it is happening / terminology of the event that may be useful to me later on.
Here is the code for the loop anyway just for reference sake:
// Main loop
while ((wrong != MAX_WRONG) && (soFar != THE_WORD))
{
char guess;
cout << "\n\n Guess a letter: ";
cin >> guess;
guess = toupper(guess); // Make uppercase to match secret word
while (used.find(guess) != string::npos)
{
cout << "\n You've already guessed '" << guess << "'.";
cout << "\n\n Guess a letter: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
if (THE_WORD.find(guess) != string::npos)
{
cout << "\n\n correct, " << guess << " is in the word.";
// Update soFar to include the correctly guessed letter
for (int i = 0; i < soFar.length(); ++i)
{
if (THE_WORD[i] == guess)
{
soFar[i] = guess;
}
}
}
else
{
cout << "\n\n wrong, " << guess << " is not in the word.";
++wrong;
cout << "\n You have " << (MAX_WRONG - wrong) << " chances to guess remaining.";
}
cout << "\n\n The word so far: ";
cout << "\n " << soFar;
}
Welcome to Hangman. Good Luck!
Guess a letter: test
correct, T is in the word.
The word so far:
________T
Guess a letter:
wrong, E is not in the word.
You have 7 chances to guess remaining.
The word so far:
________T
Guess a letter:
wrong, S is not in the word.
You have 6 chances to guess remaining.
The word so far:
________T
Guess a letter:
You've already guessed 'T'.
Guess a letter:
#include <iostream>
#include <string>
usingnamespace std;
constint MAX_WRONG{3};
int main()
{
string THE_WORD{"SELECTION"};
string soFar{};
soFar.assign(THE_WORD.length(), '_'); //<--
string used;
int wrong{0};
char guess;
while
(
cout << "Guess a letter: " &&
cin >> guess &&
wrong != MAX_WRONG &&
soFar != THE_WORD
)
{
cin.ignore(1000, '\n'); // <--
guess = toupper(guess);
while (used.find(guess) != string::npos)
{
cout << "You've already guessed '" << guess << "' \n";
cout << "Guess a letter: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
if (THE_WORD.find(guess) != string::npos)
{
cout << " ** Correct, " << guess << " is in the word.\n";
for (int i = 0; i < THE_WORD.length(); ++i)
{
if (THE_WORD[i] == guess)
{
soFar[i] = guess;
}
}
}
else
{
cout << " ** Wrong, " << guess << " is not in the word.\n";
++wrong;
cout << "You have " << (MAX_WRONG - wrong) << " chances to guess remaining.\n";
}
cout << "The word so far: " << soFar << '\n' << '\n';
}
return 0;
}
Guess a letter: e
** Correct, E is in the word.
The word so far: _E_E_____
Guess a letter: l
** Correct, L is in the word.
The word so far: _ELE_____
Guess a letter: p
** Wrong, P is not in the word.
You have 2 chances to guess remaining.
The word so far: _ELE_____
Guess a letter:
In C++ the standard input stream (cin) has a line based buffer. So when you type a bunch of characters on the keyboard all of the characters entered, up to the limit of the input buffer, are placed into the buffer to be processed by one or more of the input operators. When retrieving a single char only one character will be extracted from the stream all the rest are left in the stream for the next input operation.