1

I am creating a program that will convert Fahrenheit to Celsius and vice-versa. The code works but how do I get my textOut to display in multiple columns using Console.WriteLine in C#?

Here is my code (not including Program.cs or Input.cs)

namespace Assignment2
{
    class TemperatureConverter
    {

        public void Start()
        {

            int choice = 0;

            do
            {
                DisplayMenu();
                choice = Input.ReadIntegerConsole("           Your selection: ");

                switch (choice)
                {
                    case 1:
                        CalculateFahrenheitToCelsius();
                        break;
                    case 2:
                        CalculateCelsiusToFahrenheit();
                        break;
                    case 0: //end session (exit loop)
                        break;

                    default:
                        Console.WriteLine("Invalid option.  Choose between 0 and 2.");
                        break;
                }


            } while (choice != 0);
        }

        public void DisplayMenu()
        {
            Console.WriteLine();
            Console.WriteLine("*************************************************");
            Console.WriteLine("               MAIN MENU                     ");
            Console.WriteLine("*************************************************");
            Console.WriteLine("      Convert Fahrenheit to Celsius     : 1");
            Console.WriteLine("      Convert Celsius to Fahrenheit     : 2");
            Console.WriteLine("      Exit the Converter                : 0");
        }

        public void CalculateFahrenheitToCelsius()
        {
            double convertedValue = 0;
            string textOut = string.Empty;

            for (int i = 0; i <= 212; i += 4)
            {
                convertedValue = FahrenheitToCelsius(i);

                textOut = string.Format("{0,16:f2} F =  {1,6:f2} C", i, convertedValue);
                Console.WriteLine(textOut);
            }
            Console.WriteLine();


        }
        /// <summary>
        /// Calculate Celsius to Fahrenheit
        /// </summary>
        public void CalculateCelsiusToFahrenheit()
        {
            double convertedValue = 0;
            string textOut = string.Empty;

            for (int i = 0; i <= 100; i += 5)
            {
                convertedValue = CelsiusToFahrenheit(i);

                textOut = string.Format("{0,16:f2} C = {1,6:f2} F", i, convertedValue);
                Console.WriteLine(textOut);
            }
            Console.WriteLine();

        }


        private double FahrenheitToCelsius(double celsius)
        {
            double fahrenheit = (9.0 / 5.0) * celsius + 32.0;
            return fahrenheit;
        }

        private double CelsiusToFahrenheit(double fahrenheit)
        {

            double celsius = (5.0 / 9.0) * (fahrenheit - 32.0);
            return celsius;
        }
    }
}
Jesse
  • 3,522
  • 6
  • 25
  • 40
Corina
  • 11
  • 5
  • schoolwork? Assignment2 is the namespace??? – Any Moose Jul 23 '18 at 16:56
  • @AnyMoose Yes, this is a homework assignment though putting it in columns was not part of the assignment, I am just curious how to do it :) (the assignment is complete as is) – Corina Jul 23 '18 at 17:06
  • 1
    Your Fahrenheit and Celsius temperatures should already be aligned in columns. Can you provide an example of what you would like your output to look like? – Dan Jul 23 '18 at 17:34
  • @Dan- not sure how I can show you what I mean (comments removes the formatting once I press enter)...I just want it to display the data in two three separate columns so that the user does not have to scroll down to see all of the values – Corina Jul 23 '18 at 17:48

1 Answers1

-2

If all you're doing is writing lines directly out, you can create columns in one of two ways:

A) Include tabs.

Console.Writeline("\tColumn1\tColumn2\tColumn3");

B) If your Console is using a monospace font, you can count characters. But this would be a pain in the neck.

JamesHoux
  • 2,999
  • 3
  • 32
  • 50
  • 1
    This will only work if the contents of the columns do not exceed more than the length of one tab. – ivcubr Jul 23 '18 at 17:03
  • @ivcubr Then perhaps you should pose your own solution? If you follow the link to the duplicate question, the highest upvoted answer describes exactly the same solution. You're not offering any help here. – JamesHoux Jul 28 '18 at 03:07