Connect with Expertise | Find Experts, Get Answers, Share Insights

I cant use console.readline after console.read

 
Join Date: Feb 2010
Posts: 1
#1: Feb 10 '10
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. namespace namespace1
  3. {
  4.     class class1
  5.     {
  6.         static void Main(String[] args)
  7.         {
  8.             Console.WriteLine("What is your initial?");
  9.             int name1 = Console.Read();     
  10.             char ch = Convert.ToChar(name1);
  11.             Console.WriteLine("What is your name again?");
  12.             string name2 = Console.ReadLine();
  13.             Console.WriteLine("This is the name1 " + ch + "\nThis is the name2 " + name2);
  14.         }
  15.     }
  16. }
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 ?

✓ answered by tlhintoq

Have you tried reading the MSDN for this?
http://msdn.microsoft.com/en-us/libr...sole.read.aspx

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.

tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#2: Feb 10 '10

re: I cant use console.readline after console.read


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.
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#3: Feb 10 '10

re: I cant use console.readline after console.read


Have you tried reading the MSDN for this?
http://msdn.microsoft.com/en-us/libr...sole.read.aspx

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.
Reply

Tags
c sharp, console.read, console.readline, read, readline