2
 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?

2
  • iirc, 13 is the key code for the Enter key. As far as I can tell, Console.Read() only reads one character, not including the Enter. Anyone care to explain why he's getting 13 in hello2? Commented Dec 19, 2014 at 0:43
  • It see-m*s to be accepting two numbers I notice if i put kj it reads both k and j as separate and gives me the numbers but if i try to input separately will apply as 13 and I know why. I have to press enter after input so its assigning that second integer as enter which is 13. Idk how to fix this :/ for example in my code: int x = Console.Read(); Console.WriteLine(x); int y = Console.Read(); Console.WriteLine(y); I put mk I receive 109 and 107 the two corresponding numbers. That's great and all but i'd like to know how to have them inputted separately Commented Dec 19, 2014 at 2:33

3 Answers 3

6

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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);

5 Comments

You should use Convert.ToInt32, since you're assigning to an int. Be aware, however, that if the end user types something that isn't an int, it will raise an exception.
Thank you for correction, but differences between int32 and int16 is just about their size, am i wrong? also it is a architacture dependent term. Reason of offering int16, the resource efficiency.
I'm a noob to this sorry ;-; Reed can you please break down the code of what you wrote to me! Like what us the purpose of convert to int for console.readline? Thank you so much for all your help and fast responses!!
@OğuzhanKAYIŞ Assigning to an int converts to an Int32 anyways - so you're no more efficient.
@MattJones Console.ReadLine will return a string. You need to convert it to a number. The technique in my post will do it safely (if somebody types in something that's not a number, it won't crash). This will do it, but if a user types in "foo" instead of "3", it'll raise an exception, and if you don't handle it, tear down the process.
0

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();

1 Comment

I've used @reedcopsey s suggestions to check for numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.