-4

Basically I'm trying to not let the user input string instead of an integer; but on line of code:

else if (Convert.ToString(result) == "")

I get an error.

Full code:

class Program
{
    static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.Red;

        int calcKelvin = 273;
        int calcFahren = 32; 
        int result = Convert.ToInt32(Console.ReadLine());

        if (result == 0)
        {
            Console.WriteLine("Check it up on google!");
            Console.Title = "I'M USELESS CONSOLE, YOU CAN NOW EXIT || I'M USELESS CONSOLE, YOU CAN NOW EXIT || I'M USELESS CONSOLE, YOU CAN NOW EXIT ||";
        }
        else if (Convert.ToString(result) == "")
        {
            Console.Write("Error, you can not convert a text");
        }
        else 
        { 
            Console.WriteLine("Kelvin = " + calcKelvin * result);
            Console.WriteLine("Fahrenheit = " + calcFahren * result);
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I'm intrigued by your temperature calculation. Unless your user enters "-40", there's no way that you can take an integer can convert it both to Kelvin and to degrees Fahrenheit simply by addition. Assuming that your user enters the value in degrees Celsius, then you can add 273.15 and get a temperature in Kelvin. However, you must first divide device a degrees Celsius temperature by 5, then multiply by nine, then add 32 to get degrees Fahrenheit. If you haven't figure it out yet, you should also do this as a `double` rather than an `int`. And, 0 deg C is a perfectly good temperature! – Flydog57 Jan 14 '19 at 04:04
  • Possible duplicate of [What's the main difference between int.Parse() and Convert.ToInt32](https://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32) – mjwills Jan 14 '19 at 04:10

2 Answers2

1

The safest way to get a number from a string is to use the TryParse method, because this method returns two values! The actual return type is a bool which indicates whether or not the string was successfully converted, and the other is an out parameter, which is of the type that we're converting to, and which gets set to the converted value (or is set to the default value of the type if the conversion fails).

For temperatures, we often deal with decimal numbers, so a double is probably a good type to store the result. So, we'll use double.TryParse.

Now, since we don't necessarily want to just quit if the user makes a mistake, we should probably do our conversion in a loop, so if it fails, we just ask the user to try again. And since this code will be used in other places as well, we can make a helper method that takes in a prompt that we display to the user, and returns the strongly-typed user response:

private static double GetDoubleFromUser(string prompt = null)
{
    double result;

    do
    {
        Console.Write(prompt);
    } while (!double.TryParse(Console.ReadLine(), out result));

    return result;
}

With this method, we can now just declare a double and assign it to the return value of the method above, like:

double userInput = GetDoubleFromUser("Enter a temperature: ");

Another thing we can correct in the code are the formulas used to do the conversions. A quick check online shows us that we add a number for kelvin and we do multiplication, division, and addition for Fahrenheit. We can calculate these values on the fly once we have the Celsius temperature from the user:

private static void Main()
{
    double celcius = GetDoubleFromUser("Enter a Celcius temperature: ");

    double fahrenheit = celcius * 9 / 5 + 32;
    double kelvin = celcius + 273.15;

    Console.WriteLine("Kelvin = " + kelvin);
    Console.WriteLine("Fahrenheit = " + fahrenheit);

    GetKeyFromUser("Done! Press any key to exit...");
}

Output

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

Convert.ToInt32 throws an exception if the input string is not a number. To fix that, you can use int.TryParse instead.

Example:

using System;
class Program
{
static void Main(string[] args)
{

Console.ForegroundColor = ConsoleColor.Red;


int calcKelvin = 273;
int calcFahren = 32; 
int result;
bool isNum=int.TryParse(Console.ReadLine(),out result);

if (!isNum)
{
    Console.Write("Error, you can not convert a text");
}
else if (result == 0)
{
    Console.WriteLine("Check it up on google!");
    Console.Title = "I'M USELESS CONSOLE, YOU CAN NOW EXIT || I'M USELESS CONSOLE, YOU CAN NOW EXIT || I'M USELESS CONSOLE, YOU CAN NOW EXIT ||";
}
else { 

Console.WriteLine("Kelvin = " + calcKelvin * result);
Console.WriteLine("Fahrenheit = " + calcFahren * result);
}
}
}
Gymhgy
  • 338
  • 4
  • 11