3

I cannot read a second character with Console.Read() method. I mean I don't get any Prompt to read the second character from Keyboard. Any help please? Also, I understand that character is by default an int but we still need to convert it to char when reading from input, is it right? The code below reads the first char but terminates with the second.

public static void Main()
    {
        Console.WriteLine("The First Character?:");
        char firstChar=Convert.ToChar(Console.Read());

        Console.WriteLine("The Second Character?:");
        char secondChar=Convert.ToChar(Console.Read());
    }
Lloyd
  • 1,324
  • 7
  • 10
Jasmine Appelblad
  • 1,554
  • 3
  • 22
  • 37

4 Answers4

8

Looks like Console.ReadKey() is what you actually want.

Mark Sowul
  • 10,244
  • 1
  • 45
  • 51
4

Please see Console.Read. Specifically, this part:

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.

dtb
  • 213,145
  • 36
  • 401
  • 431
Peter Huene
  • 5,758
  • 2
  • 34
  • 35
  • So what would be the best way to read consecutive keys in a questions form? I want to have couple of questions with the answer of Y as Yes and N as No and then at last some control statements. Hope you got what I mean :) – Jasmine Appelblad Mar 02 '11 at 02:16
  • In that case, I would simply use Console.ReadLine and compare the entire line of input as a string to what your expected input should be. Also be sure to check the return value for EOF condition for whichever input method you end up using. – Peter Huene Mar 02 '11 at 02:19
  • I may has misread your last comment. If you just want 'Y' vs. 'N', Console.ReadKey will do and you can go with one of the other answers. – Peter Huene Mar 02 '11 at 02:30
  • 1
    It will block for one key of input. If you want the behavior of allowing the user to enter in text until they hit enter, then go with ReadLine and (probably case-insensitively) compare the string to "y", "yes", "n", "no", etc. – Peter Huene Mar 02 '11 at 02:40
  • well I understand that it's then not possible to implement scenario using only chars, I don't know but our lecturer asked to do so using only chars...strings are the way to go then. – Jasmine Appelblad Mar 02 '11 at 02:45
  • If your lecturer specifically said to use this input method, then I would ask him or her for clarification before deviating from the requirements. They may want the behavior of accepting a single key press after the prompt to continue. I wouldn't want you to lose points for using something outside the requirements if this is a graded assignment. – Peter Huene Mar 02 '11 at 02:49
3

Perhaps this code is closer to what you're looking for...

public static void Main()
{
        Console.WriteLine("The First Character?:");
        char firstChar = Convert.ToChar(Console.ReadKey().KeyChar);
        Console.WriteLine();
        Console.WriteLine("The Second Character?:");
        char secondChar = Convert.ToChar(Console.ReadKey().KeyChar);
}
carmbrester
  • 910
  • 1
  • 8
  • 21
2

Your second Console.Read() is consuming the carriage return terminating the first read.

Console.ReadKey is a bit friendlier to use, since it doesn't require a terminating carriage return. If you want to continue using Console.Read, you might try consuming and discarding the carriage return before the second prompt, like:

public static void Main()
{
    Console.WriteLine("The First Character?:");
    char firstChar = Convert.ToChar(Console.Read());

    Console.Read(); // consume carriage return

    Console.WriteLine("The Second Character?:");
    char secondChar = Convert.ToChar(Console.Read());

    Console.WriteLine(secondChar);
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179