1

I don't get why my integer isn't coming out correctly, Console.Read() method says it's returning an integer, why isn't WriteLine displaying it correctly?

int dimension;
dimension = Console.Read();
Console.WriteLine(""+ dimension);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 2
    What did you input? What was output? – John Saunders Dec 09 '11 at 12:53
  • A whole number. When I put in 10, it comes out as 49. I tried typecasting but it stays 49. – aurelie tass Dec 09 '11 at 12:55
  • 1
    Your code is working exactly like it should. You are reading exactly one character. If you want to read in **10** then you should be using **ReadLine()** this of course does not address the fact you code has a lot of possible problems. – Security Hound Dec 09 '11 at 13:18

7 Answers7

6

Console.Read() only returns the first character of what was typed. You should be using Console.ReadLine():

Example:

int suppliedInt;

Console.WriteLine("Please enter a number greater than zero");
Int32.TryParse(Console.ReadLine(), out suppliedInt);

if (suppliedInt > 0) {
    Console.WriteLine("You entered: " + suppliedInt);
}
else {
    Console.WriteLine("You entered an invalid number. Press any key to exit");
}

Console.ReadLine();

Additional Resources:

MSDN - Console.Read()

MSDN - Console.ReadLine()

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • 1
    `In real life, you should use Int32.TryParse() and handle the exception` But `TryParse` doesn't throw, that's the point of it. – Joren Dec 09 '11 at 12:57
  • @Joren, `exception` obviously wasn't the right word to use in that context. I've updated my answer to provide the "real world" portion of what I mentioned. – James Hill Dec 09 '11 at 13:02
  • I would do: `if(Int32.TryParse())` to check if conversion succeeded then output result, because input can be < 0 – Renatas M. Dec 09 '11 at 13:41
  • @Reniuz, you could go lots of different ways depending on the requirements (which weren't really stated in the OP). This is just a quick example to get them thinking in the right direction. – James Hill Dec 09 '11 at 13:43
  • I know :) I gave you upvote, but added note in comment that's it. – Renatas M. Dec 09 '11 at 13:46
0

The Console.Read method returns only a single character wrapped in an int, so that is only applicable if you are reading a number that is only one digit long, otherwise you'll always get only the first digit.

Since the return value of Read is actually a character, you cannot use it directly as an integer, you would need to parse it from character to integer.

But assuming that you want a number that is longer than one digit, then you really need to use Console.ReadLine instead and convert the input to an integer using int.TryParse. If int.TryParse returns false you can warn the user that he provided an invalid input and ask for the dimension again.

Sample code:

int dimension;

bool isValidDimension;
do
{
    Console.Write("Dimension: ");

    string input = Console.ReadLine();

    isValidDimension = int.TryParse(input, out dimension);

    if (!isValidDimension)
    {
        Console.WriteLine("Invalid dimension... please try again.");
        Console.WriteLine();
    }
} while (!isValidDimension);
João Angelo
  • 56,552
  • 12
  • 145
  • 147
0

From the MSDN:

Return Value

Type: System.Int32 The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.

Emond
  • 50,210
  • 11
  • 84
  • 115
0

you should it as follow

static void Main() 
{
    int Number;
    string strNumber;

    strNumber = Console.ReadLine();
    Number = int.Parse(strNumber);
    Console.WriteLine("" + dimension);
}
Mario S
  • 11,715
  • 24
  • 39
  • 47
Dewasish Mitruka
  • 2,716
  • 1
  • 16
  • 20
0

Your program is returning but you're not seeing, would you please see below code block:

You are not be able to see the output if the output window doesn't stay.

int dimension;
dimension = Console.Read();
Console.WriteLine("" + dimension);
Console.ReadLine();
Elias Hossain
  • 4,410
  • 1
  • 19
  • 33
0

Console.Read() returns ASCII code of first symbol in input. You can do

int dimension;
dimension = Console.Read();
Console.WriteLine(""+ (char)dimension);

and you'll see right first symbol in input, as

(char)dimension

will give you symbol by it's ASCII code.

igofed
  • 1,402
  • 1
  • 9
  • 15
0
int a = 0;
if(Int32.TryParse(Console.ReadLine(), out a))
{
    // Do your calculations with 'a' 
}
else
{
    // Some warnings
}
Sasha
  • 1,676
  • 1
  • 16
  • 18