Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code asks for your name and surname.

C#
class Program
{
        static void Main(string[] args)
        {
            Console.Write("Enter your name: ");
            string s = Console.ReadLine();
            Console.WriteLine("Your name: " + s);
            Console.Write("Enter your surname: ");
            int r = Console.Read();
            Console.WriteLine("Your surname: " + r);

            Console.ReadLine();
        }
}

After entering the name, the program successfully displays your input. However, after entering a surname, the program stops immediately. From my understanding, Console.Read() should return an int value of the first character of the string I enter (ASCII code?).

Why does the program terminate right after Console.Read()? Shouldn't Console.ReadLine() ensure the program stays open?

What I have tried:

if i use string r = Console.ReadLine();
then it works well.
Posted
Updated 6-Jan-17 17:45pm
v2
Comments
[no name] 6-Jan-17 15:32pm    
Catch the exception your are getting and see what it says.
Jon McKee 6-Jan-17 23:51pm    
No exception, it's a quirk of Read() if you were curious :)
ZurdoDev 6-Jan-17 16:47pm    
The big error message that you get will answer your question.
Jon McKee 6-Jan-17 23:48pm    
There's no error message. It's a quirk of Read() and why MSDN recommends not using it anymore :)
ZurdoDev 7-Jan-17 10:44am    
OK

Your program don't stay open because the last Console.ReadLine() takes the [Enter] you pressed on Console.Read() instruction.

You might want to use ReadKey() instead of Read() to achieve the desired functionality.

C#
Console.Write("Enter your name: ");
string s = Console.ReadLine();
Console.WriteLine("Your name: " + s);
Console.Write("Enter your surname: ");
string r = Console.ReadKey(false).KeyChar.ToString();
Console.WriteLine();
Console.WriteLine("Your surname: " + r);
Console.WriteLine("Press any key to continue...");

Console.ReadKey(true);


Best regards!
 
Share this answer
 
Console.Read() accepts values into the input stream until Enter is pressed. Each subsequent call to Console.Read() reads a single character from that input. Console.ReadLine() retrieves a full line from the input stream. Do you see what's going wrong?

If you type in "XYZ" and press Enter at Console.Read() you have "YZ<enter>" in the input stream with r set to X. Then Console.ReadLine() picks up the remaining "YZ<enter>" and the program terminates. This quirk is why MSDN recommends not to use Console.Read() favoring Console.ReadLine() and Console.ReadKey() instead.

This is easily demonstrated with the following code:
C#
int r = Console.Read();
Console.WriteLine(Console.ReadLine());
Console.ReadKey();

Typing "XYZ" then Enter will yield the following output:
XYZ
YZ
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900