0

I've tried writing this kind of menu in c#:

Put a vehicle in the garage ----------------------------> 1
Display Licenses of cars in garage ---------------------> 2
Change a vehicle status --------------------------------> 3
Fill tires to maximum ----------------------------------> 4
Refuel a vehicle ---------------------------------------> 5
Recharge a vehicle -------------------------------------> 6
Show data of a vehicle ---------------------------------> 7
Exit ---------------------------------------------------> 0

The only problem I have, is that I am getting all sorts of values, which are not permanent like here in this menu and so this the result of the print I get in practice:

Put a vehicle in the garage ---------------------------> 1
Display Licenses of cars in garage ----------------------------> 2
Change a vehicle status ---------------------------> 3
Fill tires to maximum ---------------------------> 4
Refuel a vehicle---------------------------> 5
Recharge a vehicle ----------------------------> 6
Show data of a vehicle ---------------------------> 7
Exit ---------------------------> 0

and well, I want it all aligned pretty like at the top of my example. Do anyone have suggestion how so I do it?

sharon
  • 21
  • 2

1 Answers1

0

You need to calculate the number of dashes to use per line. Try something like this.

using System;

public class Program
{
    public static void Main()
    {
        string[] menuPrompts = {
            "Put a vehicle in the garage", 
            "Display Licenses of cars in garage", 
            "Change a vehicle status", 
            "Fill tires to maximum", 
            "Refuel a vehicle", 
            "Recharge a vehicle", 
            "Show data of a vehicle", 
            "Exit"
        };
        int maxLength = 56;
        for (int i = 0; i < menuPrompts.Length; i++)
        {
            string prompt = menuPrompts[i];
            int n = maxLength - (prompt.Length + 1);
            string pad = new String('-', n);
            Console.WriteLine("{0} {1}> {2}", prompt, pad, i + 1);
        }
    }
}
  • Hi, thank you so much. I wanted to ask if there is a way to get the maxLength given that my Array is array of enums which the UI don't know, so I can't determine from advanced the max len – sharon Sep 09 '20 at 20:25