C# C# noob with a little problem here

  • Thread starter Thread starter stonecoldgen
  • Start date Start date
  • Tags Tags
    Noob
AI Thread Summary
The code provided is intended to check if a user-inputted number is less than 10, but it consistently outputs "false" due to the use of Console.Read(), which captures the ASCII code of the input character rather than the actual integer value. The solution suggested involves changing the input method to Console.ReadLine() and parsing the input to an integer using Int32.Parse. This adjustment ensures that the program correctly interprets the user's input as an integer, allowing for accurate comparisons and the expected output.
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?
 
Technology 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());
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top