C# noob with a little problem here

  • Context: C# 
  • Thread starter Thread starter stonecoldgen
  • Start date Start date
  • Tags Tags
    Noob
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
stonecoldgen
Messages
108
Reaction score
0
Hi, so I have this code:


Code:
namespace Program1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Is the numer less than 10?");

      

            int numero = Console.Read();

            if (numero < 10)
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }

           

        }
    }
}




The console is always writing the line "false" even if I input a number that should return "true."


any idea what am I doing wrong?
 
Physics news on Phys.org
maybe you have to cast the input into an integer as it comes in, else it converts to integer zero
 
Console.Read returns the ascii code for the key pressed, not the number itself.

Try this:
Code:
            int numero = Int32.Parse(Console.ReadLine());