0

This is a very simple bit of code. But it is the first time I have written a console app in C#. To put it simply, Using these two lines

    int iRoll;
    Console.WriteLine("Roll Dice and input number for your move");
    iRoll = Console.Read();

and if I enter the number 5 from the cmd window (it does not matter if it is from the number pad or the keyboard) the value for iRoll is 53. Why is this?

xarzu
  • 8,657
  • 40
  • 108
  • 160

2 Answers2

9

Console.Read returns an int containing the character read from the input stream. The character 5 is encoded as 53.

If you want a string containing the line you should use ReadLine instead:

string line = Console.ReadLine();

You can then parse the string into an int using int.TryParse:

int iRoll;
if (int.TryParse(line, out iRoll)) {
    // use iRoll
} else {
    // handle invalid input
}
Lee
  • 142,018
  • 20
  • 234
  • 287
1

This is because you read the ASCII code of '5' char. This is 53 value .

Przemo
  • 193
  • 2
  • 16