473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

I cant use console.readline after console.read

1
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 ?
Feb 10 '10 #1

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

2 4869
tlhintoq
3,525 Expert 2GB
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.
Feb 10 '10 #2
tlhintoq
3,525 Expert 2GB
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.
Feb 10 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alexander Stante | last post by:
Hi, I know Python since about 3 days or so and I like it very much :-) I am currently tying to control an external console driven program but I have encountered some problems while trying to do...
2
by: Paul Johnston | last post by:
I'm using VB.Net. I've tried using Console.Read() but it requires a line-terminator before it finishes (doesn't that mean it's functionally equivalent to ReadLine() -- why have it then?). I've...
7
by: ~toki | last post by:
I have a full work app (server and client) that talk between two executables trought Console.WriteLine(); & Console.ReadLine(); I have the source code of the server. I need to make the client....
11
by: William Stacey [MVP] | last post by:
Trying to test a simple console app that reads from standard in if you pipe input into it like: "c:\ type config.txt | consoleinput.exe" Seem to run into an issue. It seems like if you use a...
3
by: - Steve - | last post by:
Okay I call Console.Read() twice. However when I come up on my second Console.Read() ASCII character 10 is already on the console (that's a line feed). How do I clear out that line feed before...
5
by: Publicjoe | last post by:
I am working on a little app which uses colour in the console window. I have created a class to extend the console functionality but the ClearScreen method does not work correctly. I am enclosing a...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
7
by: mabra | last post by:
Hi All ! Problem:Reading/duplicating "Console.In" fails for unknown reason. I wrote a little console helper tool, named TEE, which just duplicates the StdIn to the StdOut AND an additional...
2
by: SriBhargav | last post by:
Hi, I've a question on setting timeout on console.readline() I would like the user to input something through Console.readline() in 5 secs. If there is no input in that time, I would like to...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.