2

I get 48 when I input 0 to a ReadLine().

Is this a bug?

class Program
{
    static void Main(string[] args)
    {
        string name;
        int age;

        readPerson(out name, out age);
    }
    static void readPerson(out string name, out int age)
    {
        Console.Write("Enter name: ");
        name = Console.ReadLine();
        Console.Write("Enter age: ");
        age = Console.Read();
        Console.WriteLine("Name: {0}; Age: {1}", name, age.ToString());
    }
}
KMC
  • 19,548
  • 58
  • 164
  • 253
  • Related post - [Console.Read() and Console.ReadLine() problems](https://stackoverflow.com/q/12308098/465053) – RBT May 24 '18 at 08:57

3 Answers3

10

No, it's not a bug at all.

Console.Read() returns the ASCII character code for whatever character is entered. The ASCII code for the digit 0 is 48, and for the digit 1 is 49, and so on. It's not adding the number 48 arbitrarily, nor does it have anything to do with out parameters.

You need to read in a line and cast the input to an integer accordingly:

Console.Write("Enter age: ");
age = Convert.ToInt32(Console.ReadLine());

If you need to use Read() for whatever reason, then like I said in my comment you need to cast the result to a char. You'll also need to change your variable from int age to char age:

class Program
{
    static void Main(string[] args)
    {
        string name;
        char age;

        readPerson(out name, out age);
    }
    static void readPerson(out string name, out char age)
    {
        Console.Write("Enter name: ");
        name = Console.ReadLine();
        Console.Write("Enter age: ");
        age = (char) Console.Read();
        Console.WriteLine("Name: {0}; Age: {1}", name, age.ToString());
    }
}

Bear in mind that Read() can only read one character at a time, so if you need to parse an age with more than one digit, this won't work, and you're much better off just using ReadLine() instead.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • thanks. It worked. Just curious: if I use Read instead of ReadLine, the statement again returns the ASCII character. Why? – KMC Feb 06 '12 at 06:38
  • It always returns the ASCII character code for the next character in the input stream. There is no "why". – BoltClock Feb 06 '12 at 06:40
  • This was not the "why" I was asking. 'Convert.ToInt32(Console.ReadLine())' works, but 'Convert.ToInt32(Console.Read())' still returns ASCII. – KMC Feb 06 '12 at 06:44
  • 1
    @KMC: I said it returns the ASCII character code. Converting it to an integer isn't going to change it because it's already an integer. If you tried to cast it to a char, though, it should return you the character itself. – BoltClock Feb 06 '12 at 06:46
  • if I use Read instead of Readline, how do I convert the ASCII to an int? – KMC Feb 06 '12 at 06:55
  • @KMC Are you trying to get the value `1` as an `int` when someone types in `1`? –  Feb 06 '12 at 07:02
7

According to the MSDN documentation, the Console.Read method returns:

The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.

So, really what you're seeing is only the first character currently on the stream (i.e., characters received between the last two Enter pushes).

During your unit testing, it appeared as if the values were shifted by 48, because it so happens that the ASCII values for the characters from '0' to '9' are, you guessed it, 48 for '0', 49 for '1', etc:

ASCII Table

Since you didn't specify a conversion, stream contents were "automagically" read as char values, and your call to Read() displayed their ASCII decimal equivalents.

You can verify this using this simple test:

static void TestRead()
{
    int current = 0;

    Console.Write("Enter 1: ");
    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    Console.Write("Enter 22: ");
    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);
}

Which will result in:

Output

You will notice that back-to-back calls to Read() grab a single character from the stream, and give you its ASCII decimal equivalent. Also, note how Windows appends a carriage return (ASCII 13) and linefeed (ASCII 10) sequence for every stroke of the Enter key, which your program faithfully echoes back to you.

A slight modification of this test method would help drive the point that lacking specific directions, the runtime will interpret the contents of your input stream as characters:

static void TestReadModified()
{
    int current = 0;

    Console.Write("Enter a: ");
    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);
}

As expected, the method above will return the ASCII value for character 'a':

Output for a letter

As others have already mentioned, this is easy to fix. Just inform the runtime that you want the value to be interpreted as an int. It's probably also a good idea to, at least, check that the input received is a number:

static void readPerson(out string name, out int age)
{
    Console.Write("Enter name: ");
    name = Console.ReadLine();

    Console.Write("Enter age: ");

    // in this case, we could simply use tempAge (defaults to 0)
    // but it's just practice to check TryParse's success flag
    int tempAge;
    var success = Int32.TryParse(Console.ReadLine(), out tempAge);

    age = success ? tempAge : 0;

    Console.WriteLine("Name: {0}; Age: {1}", name, age);
    Console.ReadLine();
}
Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
  • 1
    great answer - I can't imagine any clearer then that. I wonder how the Read() "keeps" and throw out the separate char in sequence. It can also get overwritten if there is new input. Is there some sort of buffer mechanism? Can you direct me anywhere for further read? I know it's useless, but I'm curious. – KMC Feb 06 '12 at 13:23
  • There's plenty of info around. Here are a couple of quickly grabbed articles: http://stackoverflow.com/questions/1216380/what-is-a-stream and http://msdn.microsoft.com/en-us/library/system.io.stream.aspx – Gustavo Mori Feb 06 '12 at 14:54
1

The ASCII codes for the numbers are being printed. Note that Console.Read() reads the next character from the standard input and returns an int. So, when you input character 0 ( not integer 0), it returns the ASCII code 48 and so on. Also, this ASCII code is the integer code for the character ( say 0) so converting to int of this code is redundant. You read in the age as string and convert it to integer, which can be done with Console.ReadLine and then using int.TryParse etc.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • @KMC - Your edit seems to suggest you haven't read my answer. Read gets character 0, and not integer 0 when you type. So it returns the 48, which is the ascii code for it. Note that this 48 in itself is an integer and hence converting that 48 into int will return 48 only. You are not converting character 0 into int, you are converting 48 into int. – manojlds Feb 06 '12 at 07:19
  • I thought ASCII has type Char in Utf16? Shouldn't casting char to int should implicitly convert the char code to its corresponding int? – KMC Feb 06 '12 at 07:39
  • @KMC: `Console.Read()` does not return char. – BoltClock Feb 06 '12 at 08:00