Jonas Girdzijauskas Jonas Girdzijauskas - 1 month ago 5
C# Question

second Console.Read(); is being ignored [c# language]

I've searched this question in this forum and I couldn't find the answer I needed so I'm creating a thread.

Ok, so here's the code w/o 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
Console.Read();
and before the
Console.ReadKey();


Any help please?

Console.WriteLine("Welcome to the game. What is your name?");
Console.Write("It's ");
Console.Read();
Console.WriteLine("That sounds amazing! How old are you?");
Console.Write("I'm ");
Console.Read();
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();

Answer

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.

Comments