I cant use console.readline after console.read

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elvan
    New Member
    • Feb 2010
    • 1

    I cant use console.readline after console.read

    Code:
    using System;
    namespace namespace1
    {
        class class1
        {
            static void Main(String[] args)
            {
                Console.WriteLine("What is your initial?");
                int name1 = Console.Read();     
                char ch = Convert.ToChar(name1);
                Console.WriteLine("What is your name again?");
                string name2 = Console.ReadLine();
                Console.WriteLine("This is the name1 " + ch + "\nThis is the name2 " + name2);
            }
        }
    }
    I was trying to get a character on first input then get a user full name on second input. But if i enter more than 1 character in the first input, console wont prompt me to enter second input and direct show the result shown below:
    What is your initial?
    testing
    What is your name again?
    This is the name1 t
    This is the name2 esting
    Press any key to continue . . .

    How to let the console capture the first char in the first input and prompt user to key in second input ?
    Last edited by tlhintoq; Feb 10 '10, 12:30 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Have you tried reading the MSDN for this?


      Originally posted by MSDN
      The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). 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.

      Note that you will not get a property value of -1 unless you perform one of the following actions: simultaneously press the Control modifier key and Z console key (CTRL+Z), which signals the end-of-file condition; press an equivalent key that signals the end-of-file condition, such as the F6 function key in Windows; or redirect the input stream to a source, such as a text file, that has an actual end-of-file character.

      The ReadLine method, or the KeyAvailable property and ReadKey method are preferable to using the Read method.

      Comment

      Working...