473,657 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I cant use console.readlin e after console.read

1 New Member
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
2 4877
tlhintoq
3,525 Recognized Expert Specialist
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 Recognized Expert Specialist
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
2217
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 so. Suppose I will try to control a program like a ftp command line client. At first I wrote something like: import popen2
2
4524
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 tried Console.In.ReadBlock(), too, with no success. So, along these lines, how would I implement, simply, a "Press Any Key to Continue" routine or, more complex, a text menu in a Console app to get a single character? i.e.
7
2961
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. To do this i have Console.WriteLine("Hi server"); while (true)
11
20904
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 pipe, you can read all the input lines fine until eof. However, if you need to then prompt for input, the pipe is still hooked up so Read or ReadLine returns with -1 or null after that point. So you can't prompt for "Hit a key to continue", etc....
3
12398
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 I call Console.Read() again?
5
6333
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 complete project to show what happens. If anybody has an idea of how to fix this, please let me know. Yes I am aware that this is all in .Net 2. Thanks in advance.
1
5187
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 elegant way to inject CrLf into the input stream, which effectively unblocks the ReadLine method. Last night, I had finally got the WriteConsoleInput
7
2293
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 stream. Looks working basically and the part that does this is like the following: private static void CopyStream(TextWriter outStream, bool dup) {
2
14369
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 proceed further with the program logic. I had a difficulty in implementing this, as console.readline() indefinitely waits for the user input.
12
6525
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 combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned Console.ReadLine() statement.
0
8829
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6172
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.