0

With the code below, when I write what the input is to the console, it returns 54 if I enter 6. I'm not sure why this is happening, and I would like to know.

namespace VariablePractice
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World.");
            int input = Convert.ToInt32(Console.Read());
            Console.WriteLine("You entered: " + input);
            Console.ReadKey();
        }
    }
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
kamins11
  • 1
  • 2
  • Use Int32.Parse rather than using Convert.ToInt32. Convert.ToInt32(string s) method converts the specified string representation of 32-bit signed integer equivalent. Int32.Parse (string s) method converts the string representation of a number to its 32-bit signed integer equivalent. So, therefore change your code to this: int input; Int32.TryParse(Console.ReadLine(), out input); – marak Nov 06 '17 at 02:23
  • As you can see from the duplicates (just a handful of the many similar questions already on Stack Overflow), you're not the first to fail to understand that the `char` type represents a _character code point_ and not an actual numeric value. Passing it to `Convert.ToInt32()` gives you the character code point value, not the numeric value that digit would represent. – Peter Duniho Nov 06 '17 at 02:25

0 Answers0