-1

I am learning to code with C# since I like what the language has to offer and in a learning exercise I was doing I came up with a problem.

The gist of the exercise was to learn declaring variables, using them and displaying them.

My code, which works, goes like this:

//Declare variables
        float originalFahrenheit;
        float calculatedFahrenheit;
        float calculatedCelsius;

        //Asks for fahrenheit
        Console.Write("Enter temperature in fahrenheit: ");
        originalFahrenheit = float.Parse(Console.ReadLine());

        //Calculations
        calculatedCelsius = ((originalFahrenheit - 32) / 9) * 5;
        calculatedFahrenheit = ((calculatedCelsius * 9) / 5) + 32;

        //Display calculations
        Console.WriteLine(originalFahrenheit + " degrees fahrenheit is " + calculatedCelsius + " degrees celsius");
        Console.WriteLine(calculatedCelsius + " degrees celsius is " + calculatedFahrenheit + " degrees fahrenheit");

        Console.ReadKey();

When the user inputs "70" degrees fahrenheit the answers are displayed correctly.

Enter the temperature in fahrenheit: 70

70 degrees fahrenheit is 21.11111 degrees celsius

21.11111 degrees celsius is 70 degrees fahrenheit

Now, I tried using 'casting' just because and the code compiled but the results are different:

This is the code:

        //Asks for fahrenheit
        Console.Write("Enter temperature in fahrenheit: ");
        originalFahrenheit = (float)Console.Read;

And this is the answer I got:

Enter the temperature in fahrenheit: 70

55 degrees fahrenheit is 12.77778 degrees celsius

12.77778 degrees celsius is 55 degrees fahrenheit

I would like to know why is this happening. Is casting just no good for situations like this or is my syntax bad?


One more question please. When changing:

originalFahrenheit = (float)Console.Read

to

originalFahrenheit = (float)Console.Readline()

I get an exception. See it in the picture below please:

Changing Console.Read to Console.ReadLine using casting

It basically tells me I cannot convert type 'string' to 'float'.

When changing:

originalFahrenheit = float.Parse(Console.ReadLine());

to

originalFahrenheit = float.Parse(Console.Read());

I get another exception. See it below please:

Changing 'Console.ReadLine' to 'Console.Read' using Parse

The exceptions tells me I cannot convert type 'int' to 'string'

Why does it happens? How does 'ReadLine' and 'Read' play out in the code?

Community
  • 1
  • 1
Eduardo
  • 23
  • 1
  • 6
  • 2
    Console.Read returns an integer that represents the code of a single char typed. (You can see it in the 55 value as this is the ASCII code of the char '7') To read the whole "70" number you need two calls to Read. Use Console.ReadLine – Steve May 29 '17 at 21:57
  • Oh. I understand now why 55 appears. So what about the 'cannot convert int to string'? using 'float.Parse(Console.Read())'? If the program will read just '7' because I used 'Read' why does it tells me I cannont convert int to string? I really hope I am making myself be understood here. – Eduardo May 29 '17 at 22:09
  • An cast cannot be used if it is not supported by the system or you have written an [explicit method](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/explicit) for it. Now casting an int to a float is supported (the Read case), casting a string to a float is not supported (ReadLine) the final problem (the float.Parse) is simply wrong because Parse wants a string and you pass an integer. – Steve May 29 '17 at 22:11
  • I should also add that using float.Parse to transform the user input in a float variable is very risky. If the user types something that is not a valid float number your code will throw an exception (Try to input "ABC" and see). It is a good practice to always use float.TryParse when parsing user input. – Steve May 29 '17 at 22:25
  • Really? Thank you for the tip! – Eduardo May 30 '17 at 00:17

1 Answers1

0

C# does not allow for casting from a string to a float. Such operations have to be made explicit using Float.Parse().

The reason why your code reads 55 degrees is since Console.Read() reads the next character from the input stream. That character is encoded simply as an integer, and thus casting here is allowed and you do not get an exception. Also, if you check the ASCII table - for example here http://www.asciitable.com/ - you will see that 7 (your first character) is encoded as 55.

The reason why your next piece throws an exception with Console.ReadLine() is because that function returns a string (of all characters in the input stream until the next new line character). And in such case, as we established before, C# does not allow for casting.

Float.Parse(Console.Read()) does not exactly make sense, since Float.Parse expects a string, but - as pointed out above - Console.Read() returns an int


For more information take a look at these references from MSDN: https://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx, https://msdn.microsoft.com/en-us/library/system.console.readline(v=vs.110).aspx

Michał
  • 2,202
  • 2
  • 17
  • 33