Corina
Corina

Reputation: 11

display textOut in multiple columns using C# Console.WriteLine

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;
        }
    }
}

Upvotes: 1

Views: 1333

Answers (1)

JamesHoux
JamesHoux

Reputation: 3477

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.

Upvotes: -2

Related Questions