Matt Jones
Matt Jones

Reputation: 61

C# Console.read() not displaying proper number input?

 String name = Console.ReadLine();
            System.Console.WriteLine("input name");
            System.Console.WriteLine("hello {0}", name);
            int hello = Console.Read();
            System.Console.WriteLine("First number input" + hello);
            int hello2 = Console.Read();
            Console.ReadKey();
            System.Console.WriteLine("Second number input" + hello2);

In this line of code it displays the inputted name then displays the input of the first key (i.e. hello) for the second key however no matter what I do it is always displaying as 13. How can I fix this?

Upvotes: 2

Views: 647

Answers (3)

Sam
Sam

Reputation: 2917

I found some basic issues with your code and put my thoughts in the comments below.

String name = Console.ReadLine(); // Shows the cursor without a prompt to the user
System.Console.WriteLine("input name");
System.Console.WriteLine("hello {0}", name);
int hello = Console.Read(); // This line reads the new line character (13) from the above line
System.Console.WriteLine("First number input" + hello); // Displays 13 (new line character if the user doesn't enter any value)
int hello2 = Console.Read();
Console.ReadKey(); // Exits the console as soon as a user press a key
System.Console.WriteLine("Second number input" + hello2); // This is never displays to the user

I thought of re factoring it after showing you all the issues. Here's what I've come up with. Have a look. Since you are trying to build a simple application with user inputs you must think of a flow of user inputs and outputs as well.

int firstNumber;
int secondNumber;
string name = string.Empty;

do
{
    Console.Clear();
    Console.Write("What is your name?");
    name = Console.ReadLine();        
} while (string.IsNullOrEmpty(name));

Console.WriteLine("Hello {0}", name);

do
{
    // This will happen if the user types something that's not a number
    Console.Clear();
    Console.WriteLine("Hello {0}", name);
    Console.Write("Please enter the first number:");
} 
while (!int.TryParse(Console.ReadLine(), out firstNumber));

do
{
    // This will happen if the user types something that's not a number
    Console.Clear();
    Console.WriteLine("Hello {0}", name);
    Console.WriteLine("First number is: " + firstNumber);
    Console.Write("Please enter the second number:");
}
while (!int.TryParse(Console.ReadLine(), out secondNumber));

Console.WriteLine("Second number is: " + secondNumber);
Console.Read();

Upvotes: 0

Oğuzhan KAYIŞ
Oğuzhan KAYIŞ

Reputation: 25

You can modify like that: //assumed you want to get integer values String name = Console.ReadLine(); System.Console.WriteLine("input name"); System.Console.WriteLine("hello {0}", name); int hello = Convert.ToInt16(Console.ReadLine()); System.Console.WriteLine("First number input" + hello); int hello2 = Convert.ToInt16(Console.ReadLine()); Console.ReadKey(); System.Console.WriteLine("Second number input" + hello2);

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564333

Console.Read does not parse the input character. It is misleading that it returns an integer, but this is actually a numerical representation of a single character entered.

Instead, use Console.ReadLine, and parse the results as a number.

int hello;
while(!int.TryParse(Console.ReadLine(), out hello)
{
   // This will happen if the user types something that's not a number
   Console.WriteLine("Please enter a valid number:"); 
}
Console.WriteLine("First number input" + hello);

Upvotes: 6

Related Questions