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
    2016 Award

    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());
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C# noob with a little problem here
  1. C++ Quiz starts here > (Replies: 19)

  2. A little C# help. (Replies: 7)

Loading...