Jonas Girdzijauskas Jonas Girdzijauskas - 1 year ago 86
C# Question

Second Console.Read(); is being ignored

Here is my code without the basic 'using' and namespace,

static void main
string lines (couldn't copy it good enough). The first Console.Read(); is working ok, I can type my name and it gets written good, but the second Console.Read(); is being ignored. It just writes "I'm OK. You will.. " I can't even write anything after the first
and before the

Any help please?

Console.WriteLine("Welcome to the game. What is your name?");
Console.Write("It's ");
Console.WriteLine("That sounds amazing! How old are you?");
Console.Write("I'm ");
Console.WriteLine("OK. You will be taught some basic mathematics. Get ready...");
Console.WriteLine("When you're ready, press any key!");

Answer Source

Try using Console.ReadLine() instead.

        Console.WriteLine( "Welcome to the game. What is your name?" );
        Console.Write( "It's " );
        var name = Console.ReadLine( );
        Console.WriteLine( $"Hello {name}, How old are you?" );
        Console.Write( "I'm " );
        Console.ReadLine( );
        Console.WriteLine( "OK. You will be taught some basic mathematics. Get ready..." );
        Console.WriteLine( "When you're ready, press any key!" );
        Console.ReadKey( );
        Console.Clear( );

Because my answer was selected I do want to point out Salih H. reasoning for why this was happening is correct. Console.Read only read 1 character from the stream. If more characters were entered then there would be more data in the stream, so the next time Console.Read() is called it would read the next character in the stream from the previous input.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download