Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C# noob with a little problem here

  1. Sep 25, 2012 #1
    Hi, so I have this code:


    Code (Text):
    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?
     
  2. jcsd
  3. Sep 25, 2012 #2

    phinds

    User Avatar
    Gold Member

    maybe you have to cast the input into an integer as it comes in, else it converts to integer zero
     
  4. Sep 25, 2012 #3

    DavidSnider

    User Avatar
    Gold Member

    Console.Read returns the ascii code for the key pressed, not the number itself.

    Try this:
    Code (Text):

                int numero = Int32.Parse(Console.ReadLine());
     
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook