C# loop loops the first statement three times before moving to the second statement

I have wrote this do-while loop in a console app in C#:

do 
{
    ThisHelp.ShowMenu();
    userChoice = (char) Console.Read();
    ThisHelp.Helpon(userChoice);
}while(ThisHelp.IsValid(userChoice) == false);

The method ThisHelp.ShowMenu() has just a bunch of Write + WriteLine methods, which are asking the user to type a number from 1 to 8. The method ThisHelp.Helpon() has a switch that depending on the user input passed, shows a different piece of text. The method ThisHelp.IsValid just checks that the user input is a number from 1 to 8.

Here's the problem: After the user writes a number and presses ENTER, the first statement of the loop executes 3 times before the second statement executes at least once. Why is this happening?

edit: Here is the code of the ThisHelp.IsValid method, as requested:

public bool IsValid(char ch)
{
   if (ch < '1' | ch > 8 & ch != 'q') return false;
   else return true;
}
Jon Skeet
people
quotationmark

Console.Read() generally only returns when the user has pressed return - at which point the carriage return and line feed will also be returned by subsequent calls to Console.Read(). You can validate that by logging (or examining in the debugger) the value of userChoice on each iteration.

It would probably be simpler to just call Console.ReadLine() which will consume the whole line, then convert that value to an integer.

people

See more on this question at Stackoverflow