0

The following code asks for your name and surname.

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? I am using Visual Studio 2012.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
IronLionZion
  • 123
  • 2
  • 10

2 Answers2

2

When you tell the console to enter your surname you are asking for a single character.

Console.Write("Enter your surname: ");
int r = Console.Read();

This surely should be a ReadLine followed by another ReadLine before exit. You are probably entering the first character (into Read), followed by subsequent characters, then hitting enter to accept the surname but you are actually on the ReadLine that will exit. So:

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: ");

        // change here
        string surname = Console.ReadLine();
        Console.WriteLine("Your surname: " + surname);

        Console.ReadLine();
    }
}

The program does not terminate after int r = Console.Read() for me.

Andez
  • 5,588
  • 20
  • 75
  • 116
  • While I knew ReadLine was the correct way of reading in my surname, I didn't understand why the program closed after typing in any character(s) for surname. But you were right: I was entering a string, which triggered the Read, and then pressed Return/Enter to submit the input, which triggered the ReadLine at the end, thereby ending the program. Thank you! – IronLionZion Oct 13 '15 at 06:11
1

Based on how the console application was run it will execute all the lines of code and then 'return'. Once done this will close the program as for all intents and purposes it has done what it needs to. It isn't going to sit around and be open when it has finished.

If you want it to keep the window open write Console.Readline() at the end and it will stay open, until some input has been done. I remember having this issue when I started out, and it's not a matter of the program closing unexpectedly, but rather you wanting to see the results in the console before it closes.

JEV
  • 2,494
  • 4
  • 33
  • 47
  • Thanks for the answer. I already had Console.ReadLine() at the end of my code (see above), but the problem was that I pressed enter after entering characters for my surname, which triggered both the Read() and the ReadLine() at the end. Adding a second ReadLine() keeps the program open (until I press on Return/Enter again). – IronLionZion Oct 13 '15 at 06:13
  • @user3092982 Sorry, I thought that was what you wanted to do? – JEV Oct 13 '15 at 09:43
  • I was curious about how Read() worked. Maybe I did not provide a good example, since ReadLine() is obviously the better choice for reading in my surname. I got it though, from the answer noted as "correct". Thank you once again. – IronLionZion Oct 14 '15 at 10:27