0

I am new to C#. So, I was practicing by writing some simple codes. I decided to write a code where the user will enter a number and the same number will be shown as the output. I wrote the following code and it worked perfectly.

However,when I decided to replace Console.Readline() by Console.Read() to see what would be the output, and ran the code, I found the output to be the ASCII Code of the first digit of the number that I entered. [That is when I entered 46, the output was 52.]

Whereas, when I had used Console.ReadLine(), the entire two digit number was shown.

According to me, Shouldn't it be that Console.Read() displays only the first digit of the number entered while Console.ReadLine() shows the entire number?

using System;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1;
            Console.Write("Enter a number:");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("The number is: " + num1);
            Console.ReadKey();

        }
    }
}
Rajdeep Dutta
  • 876
  • 1
  • 8
  • 16
  • Does https://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx help? – mjwills Jun 05 '18 at 06:38
  • Possible duplicate: https://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline/6825957 – Prasad Telkikar Jun 05 '18 at 06:41
  • I had searched the links you gave already, but couldn't find enough info so as to clear all my doubts. It was from there I got to know that ReadLine() reads entire strings while Read() reads only the 1st character. But my doubt is that why, on using Read(), it shows the ASCII code of the first digit while ReadLine() shows the entire number and not the ASCII Code. – Rajdeep Dutta Jun 05 '18 at 06:45
  • @RajdeepDutta: Hint: look at the return type of `Console.Read()`. It's `int`, so that it can use -1 to indicate the end of a data stream. If the value isn't -1, just cast back to `char` to get the *text* entered. – Jon Skeet Jun 05 '18 at 06:48
  • @RajdeepDutta That's exactly how these methods are designed. If you are asking "why are they designed like this" then you should probably ask the people who wrote the method. – Sweeper Jun 05 '18 at 06:53
  • Yes, the return type of Read() is int while that of ReadLine() is string. So, does this mean I don't have to use that Convert.ToInt method for Read()? [I am extremely sorry, if I am being stupid or stubborrn. I am not much familiar with programming, but decided to pursue it myself in my freetime, as it looked exciting] – Rajdeep Dutta Jun 05 '18 at 06:59
  • 1
    You seem to have posted a number of *observations* but I'm failing to find a coherent *question* to be answered here. – Damien_The_Unbeliever Jun 05 '18 at 07:03
  • Possible duplicate of [Difference between Console.Read() and Console.ReadLine()?](https://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline) – Brian Tompsett - 汤莱恩 Jun 05 '18 at 09:39

1 Answers1

0

From the documentation, Console.Read returns:

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

as an int.

The int is the ASCII value of the character read. You just have to cast to char to get the character:

int characterRead = Console.Read();
if (characterRead != -1) {
    char c = (char)characterRead;

    // if you want the digit as an int, you need
    int digit = Convert.ToInt32(c.ToString());
}

Also, note that a second call to Console.Read will read the second digit. If you want to skip this, you need a call to Console.ReadLine to clear anything that's unread.

Sweeper
  • 213,210
  • 22
  • 193
  • 313