Question Converting Celsius degrees to Fahrenheit degrees using switch

Kevin91

New member
Joined
Feb 12, 2021
Messages
2
Programming Experience
Beginner
I'm a beginner c# programmer trying to program a simple app that shows a menu to the user. When option 1 is selected by the user at run-time, the program lists values from 0 to 100 Celsius degrees converted to Fahrenheit degrees. When option 2 is chosen, the program calculates and displays a list of values between 0 and 212 degrees in Fahrenheit converted to Celsius degrees. Use a constant for the values 100 and 212 in the related method. I tried to write a for loop from 0-100 in intervals as follows: for (int i = 0; i < 100; i += 4) { Console.WriteLine(i); }: However, still wasn't able to figure out how to pass the (i) and convert it with the GetCelsiusToFahrenheitLine() method to display the converted result from 0-100. –
The problem is that I'm only getting the option entered not a converted result. This is what I have done so far:


C#:
  public TemperatureConverter()
    {
        Start();
    }

    /// <summary>
    ///
    /// </summary>
    public void Start()

    {

        while (true)

        {
            int choice = ShowMenu();
            switch (choice)
            {
                case 0:
                    return;
                case 1:

                    Console.WriteLine();
                    while (true)
                    {

                        Console.Write("Enter Celsius:");

                        Console.WriteLine();
                        if (double.TryParse(Console.ReadLine(), out double celsius))
                        {
                            Console.WriteLine(ShowTableCelsiusToFahrenheit(celsius));
                            Console.WriteLine();
                            break;
                        }
                        
                    }
                    break;
                case 2:
                    Console.WriteLine();
                    while (true)
                    {
                        Console.Write("Enter Fahrenheit:");
                        if (double.TryParse(Console.ReadLine(), out double fahrenheit))
                        {
                            Console.WriteLine(ShowTableFahrenheitToCelsius(fahrenheit));
                            Console.WriteLine();
                            break;
                        }
                    }
                    break;
                default:
                    Console.WriteLine("You must enter 1 to convert to Celsius or 2 to convert to Fahrenheit or 0 to exit!");
                    break;
            }
        }
    }

    private double CelsiusToFarenheit(double celsius) => celsius * 9.0 / 5.0 + 32.0;
    private double FahrenheitToCelsius(double fahrenheit) => (fahrenheit - 32.0) * 5.0 / 9.0;
    private string ShowTableCelsiusToFahrenheit(double celsius) => $"{celsius}°C is {CelsiusToFarenheit(celsius)}°F";
    private string ShowTableFahrenheitToCelsius(double fahrenheit) => $"{fahrenheit}°F is {FahrenheitToCelsius(fahrenheit)}°C";

    public int ShowMenu()
    {
        int width = 28;
        while (true)
        {
            Console.WriteLine("MAIN MENU");
            Console.WriteLine("".PadLeft(width, '-'));
            Console.WriteLine("Celsius to Fahrenheit".PadRight(width - 3) + ": 1");
            Console.WriteLine("Fahrenheit to Celsius".PadRight(width - 3) + ": 2 ");
            Console.WriteLine("Exit the program".PadRight(width - 3) + ": 0 ");
            Console.WriteLine("".PadLeft(width, '-'));
            Console.WriteLine();
            Console.WriteLine("Your choice: ");
            string line = Console.ReadLine();
            if (int.TryParse(line, out int choice))
            {
                return choice;
            }
        }
    }

}
 
It's a method like any other. You know how to call a method and pass a parameter, right? You're already doing that plenty of times in that code. That's all you do there too. Call the method, pass the argument and it will return the result. This:
C#:
private double CelsiusToFarenheit(double celsius) => celsius * 9.0 / 5.0 + 32.0;
is just a more succinct way to write this:
C#:
private double CelsiusToFarenheit(double celsius)
{
    return celsius * 9.0 / 5.0 + 32.0;
}
 

Latest posts

Back
Top Bottom