-2

Trying to write a simple set of code that converts from fahrenheit into celsius, and there are three set conditions that determines what happens. Either it's too cold, just right or too hot. For some reason no matter the input, it will only reply and say it is too cold. I can't really figure out why. Here's the code so far;

{
    class Program
    {

        static int FahrenheitToCelsius(int fahrenheit)
        {
            int celsius = ((fahrenheit - 32) * 5 / 9);
            return celsius;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the desired temperature: ");
            int fahrenheit = Convert.ToInt32(Console.ReadLine());
            int celsius = FahrenheitToCelsius(fahrenheit);

            while (celsius != 75)
                if (celsius < 73)
                {
                    Console.WriteLine("Too cold! Please enter a warmer temperature.");
                    Console.ReadLine();
                }
                else if (celsius > 77)
                {
                    Console.WriteLine("Too warm! Please enter a colder temperature.");
                    Console.ReadLine();
                }
                else if (celsius == 75)
                {
                    Console.WriteLine("Optimal input! Begin heating up.");
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid input! Please input a temperature.");
                }
        }
    }
}
Jim G.
  • 15,141
  • 22
  • 103
  • 166
  • 1
    do you think maybe you should ask the question and get the input *inside* the loop? this might be a good time to use `do`/`while` – Marc Gravell Nov 18 '20 at 12:32
  • Hint: you never update the celsius value (or fahrenheit, for that matter). A ReadLine will just wait for you to hit enter, nothing more, unless you use the value. – Hans Kesting Nov 18 '20 at 12:32
  • You should move input inside `while` too. Currently it's infinite loop for anything except `75`. – Sinatr Nov 18 '20 at 12:32
  • Does this answer your question? [Reading an integer from user input](https://stackoverflow.com/questions/24443827/reading-an-integer-from-user-input) – Drag and Drop Nov 18 '20 at 12:34
  • 1
    You know how to get an user input you already did it once and put it into a variable `int fahrenheit = Convert.ToInt32(Console.ReadLine());` But 5 lines later you expect `Console.ReadLine();` to do more that just read and forgeting the value – Drag and Drop Nov 18 '20 at 12:35
  • As @HansKesting already pointed out, the celsius variable is never updated. Furthermore even if celsius variable gets updated the second else if will never be true as the while condition will be met in this case – rufer7 Nov 18 '20 at 12:36
  • And check your conversion: you don't want integer division there – Hans Kesting Nov 18 '20 at 12:36
  • And The first time you have to enter a value in fahrenheit. Don't forget to convert the user input to fahrenheit.. – Drag and Drop Nov 18 '20 at 12:38
  • And you have an issue with your If: T <= 163 is too cold; T = 164,165,166,169,170 are all invalid Temperature; T >= 171 is too hot; – Drag and Drop Nov 18 '20 at 13:24

2 Answers2

0

maybe You only getting same message because you haven't change value of celsius

static int FahrenheitToCelsius(int fahrenheit)
        {
            int celsius = ((fahrenheit - 32) * 5 / 9);
            return celsius;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the desired temperature: ");
            int fahrenheit = Convert.ToInt32(Console.ReadLine());
            int celsius = FahrenheitToCelsius(fahrenheit);
            Console.WriteLine("C = " + celsius);
            int i = 0;
            while (celsius != 75) {

                if (i>0)
                {
                    int x = Convert.ToInt32(Console.ReadLine());
                    celsius = FahrenheitToCelsius(x);
                }
                 
            
                if (celsius < 73)
                {
                    Console.WriteLine("Too cold! Please enter a warmer temperature.");
                    

                }
                else if (celsius > 77)
                {
                    Console.WriteLine("Too warm! Please enter a colder temperature.");
             
               
                }
                else if (celsius == 75)
                {
                    Console.WriteLine("Optimal input! Begin heating up.");
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid input! Please input a temperature.");
               
                }
         
               
                i++;
            }
        }
    }
  • Thats a weird usage of a counter. It's a do/while that way you won't need to increment a int in order to have a boolean flag.. – Drag and Drop Nov 18 '20 at 13:08
0

First, Fix your conversion. When converting from one unit to an other one should not remove the decimal value.

static double FahrenheitToCelsius(double f) => (f - 32) * 5.0 / 9.0;

Now lets talk about your if/else. In your code

T <= 163 is too cold;
T = 164,165,166,169,170 are all invalid Temperature;
T >= 171 is too hot;

There is not reason to have those invalid right in the middle of the range.
And there is no explanation on Invalid temp so just drop it.

Is there a number that can satisfy multiple of those condition? x < 73, x > 77, x ==75...
We can safely drop all the else.

if (tempC < 73)
{
    Console.WriteLine("Too cold! Please enter a warmer temperature.\n");
}
if (tempC > 77)
{
    Console.WriteLine("Too warm! Please enter a colder temperature.\n");
}
if (tempC == 75)
{
    Console.WriteLine("Optimal input! Begin heating up.\n");
}

Using a Do/While loop we have :

static void Main(string[] args)
{
    double tempC , tempF;
    do
    {
        Console.WriteLine("Please enter the desired temperature: ");
        tempF = Convert.ToDouble(Console.ReadLine());
        tempC = FahrenheitToCelsius(tempF);

        Console.WriteLine($"{tempF,4:F}°F, {tempC,4:F}°C");

        if (tempC < 73)
        {
            Console.WriteLine("Too cold! Please enter a warmer temperature.\n");
        }
        if (tempC > 77)
        {
            Console.WriteLine("Too warm! Please enter a colder temperature.\n");
        }
        if (tempC == 75)
        {
            Console.WriteLine("Optimal input! Begin heating up.\n");
        }

    }
    while (tempC != 75);
}

Nb renamed the variable from Fahrenheit and Celsius to tempF and tempC.
Temperatur unit find is the while are : C, F, K, R, De, N, Re, Ro.
I'm not sure one can write the name of those without google.

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37