C#: if a user hits more than one char, then (do something)

I am working on a hangman game still, I would like some hints from this community again.

My code works as follows at the moment: if a user types only one char and hits enter there are two possible outcomes:
if the typed char matches a word in a file, then it returns a matched word.
if not, nothing happens.

On top of this, I would like to add some error messages if more than one char has been entered.

here’s what I have tried - it was supposed to return the error message “one char at a time” but actually nothing happens. I truly appreciate any hints or advice as I kept looking for the fix to no avail.

while (true)
                {
                    string playerGuess;
                    playerGuess = Console.ReadLine();

                    if (playerGuess.Length > 2)
                    {
                        Console.WriteLine("one character at one time please");
                    }
                    char guessed = playerGuess[0];
                    
                    for (int j = 0; j < mysteryWord.Length; j++)
                    {
                        if (guessed == mysteryWord[j])
                        {
                           hangman[j] = guessed;
                           amountOfLettersRevealed++;

                        } 
                        
                        
                        
                        
                    }
                    Console.WriteLine(hangman);

                    if (hangman.Length == amountOfLettersRevealed)
                    {
                        Console.WriteLine("hip hip hurray");
                    }

                    
                    
                }
1 Like

Instead of:

if (playerGuess.Length > 2)

Use this conditional expression:

if (playerGuess.Length > 1)

The issue with your conditional is that it is checking for more than 2 characters rather than checking for more than 1 char, hence why the expected output is not being shown/hit for only more than 1 char input(setting breakpoints in the future can also help with identifying this kind of issue). Hope this helps.

1 Like

Thank you so much :slight_smile: It helped indeed

1 Like