user3311305
user3311305

Reputation: 1

Looping Switch Statements

I'm trying to determine the best way to get the following code to iterate until the user enters a valid number(1-6). As long as the user inputs a number that is greater than 6, the program should continue to prompt the user for a valid number.

    static void Main(string[] args)
    {
          string favoriteBand;

        Console.WriteLine("What is your favorite rock band?");
        Console.WriteLine("");
        Console.WriteLine("1.) Journey");
        Console.WriteLine("2.) Boston");
        Console.WriteLine("3.) Styx");
        Console.WriteLine("4.) Kansas");
        Console.WriteLine("5.) Foreigner");
        Console.WriteLine("Press 6 to exit the program.");
        Console.WriteLine("");
        favoriteBand = (Console.ReadLine());

        switch (favoriteBand)
        {
            case "1": Console.WriteLine("Don't Stop Believin'!"); break;            
            case "2": Console.WriteLine("More Than a Feeling!"); break;
            case "3": Console.WriteLine("Come Sail Away!"); break;
            case "4": Console.WriteLine("Dust in the Wind!"); break;
            case "5": Console.WriteLine("Hot Blooded!"); break;
            case "6": return;
            default: Console.WriteLine("Error, invalid choice. Please choose a valid number."); break;

        }

        Console.WriteLine("Press any key to continue.");

        Console.ReadKey();

    }

How can I get this program to continue prompting for a valid number using a loop?

Upvotes: 0

Views: 117

Answers (2)

Steve
Steve

Reputation: 216273

Just for fun, here we can remove the switch
(not really needed for a simple switch with only 6 cases)

bool isValid = true;

Dictionary<string, Action> command = new Dictionary<string, Action>();
command.Add("1", () => {Console.WriteLine("Don't Stop Believin'!");isValid = false;});
command.Add("2", () => {Console.WriteLine("More Than a Feeling!");isValid = false;});
command.Add("3", () => {Console.WriteLine("Come Sail Away!");isValid = false;});
command.Add("4", () => {Console.WriteLine("Dust in the Wind!");isValid = false;});
command.Add("5", () => {Console.WriteLine("Hot Blooded!");isValid = false;});
command.Add("6", () => isValid = false);

while(isValid)
{
    string line = Console.ReadLine();
    if(command.Keys.Contains(line)) 
        command[line].Invoke();
    else
        Console.WriteLine("Choose from 1 to 6");
}

Upvotes: 3

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

Use a bool variable with while

  bool control = true;
  while(control)
  {
    favoriteBand = (Console.ReadLine());
    switch (favoriteBand)
    {
        case "1": Console.WriteLine("Don't Stop Believin'!"); control = false;break;            
        case "2": Console.WriteLine("More Than a Feeling!");control = false; break;
        case "3": Console.WriteLine("Come Sail Away!"); control = false;break;
        case "4": Console.WriteLine("Dust in the Wind!"); control = false;break;
        case "5": Console.WriteLine("Hot Blooded!"); control = false;break;
        case "6": control = false; break;
        default: Console.WriteLine("Error, invalid choice. Please choose a valid number.");  break;

    }    
  }

Upvotes: 2

Related Questions