C# noob with a little problem here

  • C/++/#
  • Thread starter stonecoldgen
  • Start date
  • #1
107
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?
 

Answers and Replies

  • #2
phinds
Science Advisor
Insights Author
Gold Member
16,836
7,711
maybe you have to cast the input into an integer as it comes in, else it converts to integer zero
 
  • #3
DavidSnider
Gold Member
500
141
Console.Read returns the ascii code for the key pressed, not the number itself.

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

Related Threads on C# noob with a little problem here

  • Last Post
Replies
11
Views
3K
  • Last Post
Replies
10
Views
3K
Replies
1
Views
2K
Replies
3
Views
2K
  • Last Post
Replies
7
Views
2K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
19
Views
14K
Replies
2
Views
2K
Replies
8
Views
3K
  • Last Post
Replies
6
Views
3K
Top