0

I am new to coding in general, and especially new to C#. I am trying to write a program that asks the user if they want to convert a temperature to Fahrenheit or Celsius. The issue is, the math isn't checking out

This is what I have so far, and from what I know, it should work:

//declare variables
double temp = 0;
double newTemp = 0;
string? cf = "";

//ask if they want to covert to celcius or farenheit
Console.WriteLine("What are you converting to? Enter c for celsius or f for farenheit:");
cf = Console.ReadLine();

//if statement and output
if(cf == "c")
{
    Console.WriteLine("Converting to Celsius. Enter temperature in farenheit:");
    temp = Convert.ToDouble(Console.ReadLine());
    newTemp = (temp - 32) * (5/9);
    Console.WriteLine("Your temperature in celcius is " + newTemp + " degrees celsius.");
}
else if(cf == "C")
{
    Console.WriteLine("Converting to Celsius. Enter temperature in farenheit:");
    temp = Convert.ToDouble(Console.ReadLine());
    newTemp = ((temp - 32) * (5/9));
    Console.WriteLine("Your temperature in celcius is " + newTemp + " degrees celsius.");
}
else if(cf == "f")
{
    Console.WriteLine("Converting to Farenheit. Enter temperature in celsius:");
    temp = Convert.ToDouble(Console.ReadLine());
    newTemp = (temp * (9/5) + 32);
    Console.WriteLine("Your temperature in farenheit is " + newTemp + " degrees farenheit.");
}
else if(cf == "F")
{
    Console.WriteLine("Converting to Farenheit. Enter temperature in celsius:");
    temp = Convert.ToDouble(Console.ReadLine());
    newTemp = (temp * (9/5) + 32);
    Console.WriteLine("Your temperature in farenheit is " + newTemp + " degrees farenheit.");
}
else
{
    Console.WriteLine("That is not celcius or farenheit. Please enter either c or f next time.");
    Environment.Exit(0);
}

For some reason that I cant figure out, the math is always wrong. -40 degrees is never equal to -40 degrees in the inverse temperature, meaning that the math is incorrect. Would anyone be able to explain to me, in beginners terms, what the issue is and how I might resolve it?

P.S. - in many forums I looked at first, they used a term called floating-point that I don't really understand. If you use this in your answer, I would really appreciate an explanation on what it means.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132

1 Answers1

4

Calculation the division 9/5 will treat both values as integer. This means the result is another integer which is 1 in this case. To ensure the calculation is done like expected at least on of the numbers needs to be a double like 9.0/5. This results in the expected value of 1.8.

See this question for more details about this.

Another hint: You do not need to copy the whole code for lower and upper case input like

if (cf == "f")
{
    //Code
}
else if (cf == "F")
{
    //The same code
}

Just simply could make use of the or operator like

if (cf == "f" || cf == "F")

or use to lower

if (cf.ToLower() == "f")
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49