Curious about the process the computer is using to handle inputting multiple letters into a char.

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.
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

// 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:



cin.ignore clears the input stream which you guessed at. I have my doubt that would fix it anyway.

However, at first glance it appears unusual to me that you have 2 cin >> guess statements and that might be where the anomaly occurs.
Last edited on
Here's an obligation-free few tips that might be of interest:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>

using namespace std;

const int 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: 
Last edited on
BTW: for info on streams and functions like ignore(...) use the reference section on this site (or others online - there are many, many)

http://www.cplusplus.com/reference/istream/istream/ignore/
Last edited on
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.

Registered users can post here. Sign in or register to post.