2

I am new to C# and coding in general so please forgive me. I want to use a while loop in the default of my switch statement but I have no clue how to setup the bool for the while. My goal is to have the menu loop until either 1 or 2 is pressed.

switch (userChoice1)
{
    case "1":
        msg = "\n\nYou have chosen the House Salad with Ranch Dressing. \nPress enter to continue.";
        saladChoice = "House Salad with Ranch Dressing";
        break;

    case "2":
        msg = "\n\nYou have chosen the Caesar Salad. \nPress enter to continue. ";
        saladChoice = "Caesar Salad";
        break;

    default:
        msg = "\n\nYou have chosen an invalid option. You should have selected  \n1 for the House Salad \nor \n2 for the Caesar Salad. ";

        Console.Beep();
        while (true) // I don't know if the switch should be in the while loop or here
        {
        }
        break;
}
Craig W.
  • 17,838
  • 6
  • 49
  • 82
NewbMilo
  • 49
  • 4
  • 1
    while loop should be outside the switch – Jake Jul 06 '15 at 17:22
  • 2
    possible duplicate of [Break out of a while loop that contains a switch statement](http://stackoverflow.com/questions/1987379/break-out-of-a-while-loop-that-contains-a-switch-statement) – Jake Jul 06 '15 at 17:24

5 Answers5

5

Something like:

    bool valid;
    do
    {
        valid = true;
        userChoice1 = Console.ReadLine();
        switch (userChoice1)
        {
            case "1":
                msg = "\n\nYou have chosen the House Salad with Ranch Dressing. \nPress enter to continue.";
                saladChoice = "House Salad with Ranch Dressing";
                break;
            case "2":
                msg = "\n\nYou have chosen the Caesar Salad. \nPress enter to continue. ";
                saladChoice = "Caesar Salad";
                break;
            default:
                msg = "\n\nYou have chosen an invalid option. You should have selected  \n1 for the House Salad \nor \n2 for the Caesar Salad. ";
                valid = false;
                Console.Beep();
                break;
        }
    } while (!valid);
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
3

First off, never write while (true) without seriously knowing what you are doing. The application/thread will not terminate correctly in such a loop.

Second, you need to loop outside the switch on some bool, something like:

bool validSelection = false;
while (!validSelection)
{
    string userSelection = Console.ReadLine();
    switch (userSelection)
    {
        case "1":
           validSelection = true;
           break;
        default:
           break;
    }
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
2

You will need to have the while loop outside the switch...

bool validVal = false;
while (!validVal )
    {
    switch (userChoice1)
        {
        case "1":
            msg = "\n\nYou have chosen the House Salad with Ranch Dressing. \nPress enter to continue.";
            saladChoice = "House Salad with Ranch Dressing";
            validVal = true;
            break;
        case "2":
            msg = "\n\nYou have chosen the Caesar Salad. \nPress enter to continue. ";
            saladChoice = "Caesar Salad";
            validVal = true;
            break;
        default:
            msg = "\n\nYou have chosen an invalid option. You should have selected  \n1 for the House Salad \nor \n2 for the Caesar Salad. ";
            Console.Beep();
            break;
        }
    }
bowlturner
  • 1,968
  • 4
  • 23
  • 35
1

Your switch should be inside the while loop. The variable the while loop looks at should be declared outside the while loop, and it's what you'll manipulate to determine whether execution will proceed past the while loop.

bool proceed = false;
while(!proceed)
{
   //probably get userChoice1 from console here?
   switch (userChoice1)
   {
        case "1":
            msg = "\n\nYou have chosen the House Salad with Ranch Dressing. \nPress enter to continue.";
            saladChoice = "House Salad with Ranch Dressing";
            proceed = true;
            break;
        case "2":
            msg = "\n\nYou have chosen the Caesar Salad. \nPress enter to continue. ";
            saladChoice = "Caesar Salad";
            proceeed = true;
            break;
         default:
             msg = "\n\nYou have chosen an invalid option. You should have selected  \n1 for the House Salad \nor \n2 for the Caesar Salad. ";
             Console.Beep();               
             break;
    }
}
mason
  • 31,774
  • 10
  • 77
  • 121
  • I'm missing something because I am now getting an infinite beep and not a choice to reselect when I press anything but 1 or 2 – NewbMilo Jul 06 '15 at 17:42
  • @NewbMilo See in my answer where I have the comment? Probably need something like `userChoice1 = Console.ReadLine();` – mason Jul 06 '15 at 18:12
1

I don't know C#, so please forgive syntactic mistakes. You could put the switch statement in a function getting rid of the while loop entirely and call this function in your default case recursively.

public void MenuSwitch()
{
   switch (userChoice1)
   {
        case "1":
            ...........
            break;
        case "2":
            ...........
            break;
        default:
            ...........
            MenuSwitch();
            break;
    }
}
haffla
  • 1,056
  • 8
  • 15
  • This has little to do with 'while' loop. It uses recursion but good point anyway. – Fabjan Jul 06 '15 at 17:37
  • You're right. Just pointing to the fact that the while loop can be replaced entirely with recursion as you pointed out correctly. I edited my answer. – haffla Jul 06 '15 at 17:44
  • Interesting concept, but it does open you up to a stack overflow situation. – David Peters Jul 06 '15 at 17:47
  • @DavidPeters Assuming there is a user choosing the wrong option a couple of thousand times in a row, you are right. Quite unlikely. What you're saying is: "Never use recursion!". – haffla Jul 06 '15 at 18:06