-2

Im am trying to solve an issue. My teacher wants us to do a C# console with temperature converter from Celsius to Fahrenheit and switch. The problem is I continue getting errors in my coding and I don't know where I started wrong in order to get completely lost. Just to mention I am in the beginning and I would appreciate some help.

We should write a program that displays a list of temperatures in Celsius converted to Fahrenheit and vice versa. A menu is provided for the user to choose the type of conversions: The menu should repeat until the user chooses 0 to exit.

When option 1 is chosen, the program calculates and displays a list of values between 0 and 212 degrees in Fahrenheit converted to Celsius degrees as shown in the next image When option 2 is selected, the program lists values from 0 to 100 Celsius converted to Fahrenheit degrees.

This is my beginning:

using System;

namespace TempconverterA2
{
    class TemperatureConverter
    {
        public void Start()
        {

            Console.WriteLine("**************************************");
            Console.WriteLine("\t      MAIN MENU");
            Console.WriteLine("**************************************");
            Console.WriteLine(" Convert Fahrenheit to Celsius : 1");
            Console.WriteLine(" Convert Celsius to Farenheit : 2");
            Console.WriteLine(" Exit the Converter : 0");
            Console.WriteLine("**************************************");

            Console.WriteLine("\nYour choice: ");
            switch (choice)
            {
                case 1:
                    CalculateFarenheitToCelsius(F = 9 / 5 * C + 32);
                    break;
                case 2:
                    CalculateCelsiusToFarenhet(C = 5 / 9 * (F - 32));
                case 0: //do nothing (exists to loop)
                    break;

                default:
                    Console.WriteLine("Invalid option. Choose between 0 and 2.");
                    break;
            } while (choice != 0) ;
            public void CalculateCelsiusToFarenhet()
            {
                double convertedValue = 0;
                stringtextOut = string.Empty;

                for (int i= 0, i <= 100; i += 5)
                {
                    convertedValue = CalculateCelsiusToFarenhet(i);
                    textOut = string.Format("{0,16:f2} C = {1,6:f2} F", i, convertedValue);
                    Console.WriteLine(textOut);

                }
                Console.WriteLine();
    }
}//End of Start
Jeff
  • 7,504
  • 3
  • 25
  • 34
Dia Di
  • 1
  • 1
    Dia Di, are you assigning anything to "choice" before using it in "switch"? It is currently probably declared as int somewhere up and defaults to 0, so ultimately it is just doing to "default" of the switch.. a guess without looking at complete cs file. – fluidguid Feb 18 '20 at 19:34
  • What's the definition of `CalculateFarenheitToCelsius()` ? Also, you're passing a parameter into `CalculateCelsiusToFarenhet()` but the method doesn't accept a parameter. – devlin carnate Feb 18 '20 at 19:37
  • 2
    couple obvious errors here: 1. you did not declare choice (compile error). 2. you did not assign a value to choice from user input (run time bug). 3. CalculateFarenheitToCelsius function doesn't exist (compile error). 4. you are missing a closing bracket after the while statement (compile error). 5. you are calling CalculateCelsiusToFarenhet with a param even though the function doesn't take a parameter (compile error). 6. you are calling CalculateCelsiusToFarenhet within CalculateCelsiusToFarenhet without a base case. this will result in an endless recursion (run time stack overflow) – Steve Feb 18 '20 at 19:42
  • I would suggest you to go thru the beginner guide again and start off from a one main function program before trying out OOP. – Steve Feb 18 '20 at 19:44
  • I'm going to agree with @Steve here. Except from OOP practices you're also missing functional ones. See some beginner videos on a functional programming language (like C). – NickDelta Feb 18 '20 at 19:55

1 Answers1

0

This is not doing what you think it is:

switch (choice)
{
    // case statements removed for brevity          
} while (choice != 0);

It is actually doing this:

switch (choice)
{

}

while (choice != 0)
{
    // infinite loop if choice != 0
}

This is because switch is a control block and while is a separate control block.

keith
  • 5,122
  • 3
  • 21
  • 50