Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While am running the following code there is no error, but it doesnt accept second input.

C#
Console.Write("Enter first char ");
          char a = Convert.ToChar(Console.Read());
          Console.Write("Enter second char ");
          char b = Convert.ToChar(Console.Read());// this input cannot be accepted
          Console.WriteLine("a-" + a.ToString() + ",b-" + b.ToString() + "");
          Console.ReadLine();



Sample Output:

Enter first char c
Enter second char a-c,b-
Posted
Comments
lukeer 11-Jul-13 7:28am    
What was the input? Was it "d"?
Suresh Cires 11-Jul-13 7:35am    
i want get char input from user..
For example
Enter first char C
Enter second char S
and the output will be a-C,b-S like that
[no name] 11-Jul-13 7:29am    
Probably because "a-c,b-" is not a char.

If you ever want to read only a single character don't use Read() as you'll be getting more problems with it than it's good for you. Console.ReadKey() is the way to go in your case:

C#
Console.Write("Enter first char: ");
char a = Console.ReadKey().KeyChar;
Console.Write("\nEnter second char: ");
char b = Console.ReadKey().KeyChar;
Console.WriteLine("\na-" + a.ToString() + ",b-" + b.ToString() + "");
Console.ReadLine();


While read will fetch the next character from the input stream, it will also block until a new line is entered. This new line will remain in the input queue so your subsequent Read() will fetch that. See below for what MSDN has to say about Read().

The MSDN documentation has this to say:
"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."


Regards,
— Manfred
 
Share this answer
 
Comments
JayantaChatterjee 11-Jul-13 9:22am    
My vote of 5+...
C#
Console.Write("Enter first char ");
           char a = Convert.ToChar(Console.Read());
           Console.ReadLine();
           Console.Write("Enter second char ");
           char b = Convert.ToChar(Console.Read());// this input cannot be accepted
           Console.WriteLine("a-" + a.ToString() + ",b-" + b.ToString() + "");
           Console.ReadKey(true);
 
Share this answer
 
Hi,

Place a break point in the start and debug line by line you can see the output. otherwise add the Console.Read() to display the expected result.

C#
Console.Write("Enter first char ");
char a = Convert.ToChar(Console.Read());
Console.Write("Enter second char ");
Console.Read(); // Add this line you can see the expected output
char b = Convert.ToChar(Console.Read());// this input cannot be accepted
Console.WriteLine("a-" + a.ToString() + ",b-" + b.ToString() + "");
Console.ReadLine();


Thanks
Sriram.B
 
Share this answer
 
Hi,

I misunderstood your question i think please check this below link it is explained clearly why the second character is not accepted. The below link mathces your requirement exactly.

http://stackoverflow.com/questions/5162670/reading-two-characters-in-c-sharp

Thanks,
Sriram.B
 
Share this answer
 
Hello,

Use ReadLine() instead of Read()

string a, b;
Console.Write("Enter first char :");
a = Console.ReadLine();
Console.Write("Enter second char :");
b = Console.ReadLine();
Console.WriteLine("a-" + a + ",b-" + b + "");
Console.ReadLine();
 
Share this answer
 
Comments
Priyanka7777 11-Jul-13 8:16am    
This works!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900