Jonas Girdzijauskas
Jonas Girdzijauskas

Reputation: 21

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 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();

Upvotes: 1

Views: 665

Answers (3)

Salih H.
Salih H.

Reputation: 102

The Read method blocks its return while you type input characters; it terminates when you press the Enter key.

Main difference between Console.Read() and Console.ReadLine() is that Console.Read() will only take one character from sequence and Console.ReadLine() will take the whole line, so in your case if you do something like this:

int x;
Console.WriteLine("Welcome to the game. What is your name?");
Console.Write("It's ");
x = Console.Read();
Console.WriteLine("Char: " + Convert.ToChar(x)+";");
x = Console.Read();
Console.WriteLine("Char: " + Convert.ToChar(x)+";");

You'll notice that you got only one char and because you entered more characters your 2nd Console.Read() will take next char from sequence and return the 2nd character from your input, so i guess you should use Console.ReadLine().

Upvotes: 2

WBuck
WBuck

Reputation: 5511

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.

Upvotes: 1

Max Young
Max Young

Reputation: 1662

I just tested this and I am seeing the same functionality you are seeing. I am not really certain why it is exiting right after pressing return. Console.ReadLine() functions the way I would have expected your code to work though.

I believe this documentation states the reason the second call to Console.Read() terminates immediately.

Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.

Upvotes: 0

Related Questions