0

Anyway I don't know how I'm supposed to go about doing this... in my application I need to have the switch repeat itself if there's an invalid input. All the application does is exit as soon as I enter a different result. Here's my code:

string str = Console.ReadLine(); 
char option = char.Parse(str);
//Need to repeat this switch:
switch (option)
{
    case 'N':
        Console.WriteLine("Creating New App...");
        break;
    case 'L':
        Console.WriteLine("Loading App...");
        break;
    case 'O':
        Console.WriteLine("Loading Options...");
        break;
    case 'Q':
        Console.WriteLine("Exiting Application...");
        System.Environment.Exit(1);
        break;
    default:
        Console.WriteLine("Invalid input.");
        break;
}
Console.ReadLine();
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Blucyrik
  • 5
  • 3

4 Answers4

3

Put the switch in a loop

bool invalidInput = true;

while (invalidInput)
{
    string str = Console.ReadLine(); 
    char option = char.Parse(str);

    switch (option)
    {
        case 'N':
            Console.WriteLine("Creating New App...");
            invalidInput = false;
            break;
        case 'L':
            Console.WriteLine("Loading App...");
            invalidInput = false;
            break;
        case 'O':
            Console.WriteLine("Loading Options...");
            invalidInput = false;
            break;
        case 'Q':
            Console.WriteLine("Exiting Application...");
            System.Environment.Exit(1);
            break;
        default:
            Console.WriteLine("Invalid input.");
            break;
    }
}
Guy
  • 46,488
  • 10
  • 44
  • 88
2

You could make use of a while statement.

bool shouldRun = true;

while(shouldRun)
{
    switch (option)
    {
        case 'N':
            Console.WriteLine("Creating New App...");
            shouldRun = false;
            break;
        case 'L':
            Console.WriteLine("Loading App...");
            shouldRun = false;
            break;
        case 'O':
            Console.WriteLine("Loading Options...");
            shouldRun = false;
            break;
        case 'Q':
            Console.WriteLine("Exiting Application...");
            System.Environment.Exit(1);
            break;
        default:
            Console.WriteLine("Invalid input.");
            shouldRun = true;
            break;
    }
}
Christos
  • 53,228
  • 8
  • 76
  • 108
1

To repeat the switch you need to use one loop.I think you are looking for something like the following:

    bool ExitFlag = true;
    while (ExitFlag)
    {
        ExitFlag = false;
        switch (option)
        {
            case 'N':
                Console.WriteLine("Creating New App...");
                break;
            case 'L':
                Console.WriteLine("Loading App...");
                break;
            case 'O':
                Console.WriteLine("Loading Options...");
                break;
            case 'Q':
                Console.WriteLine("Exiting Application...");
                System.Environment.Exit(1);
                break;
            default:
                Console.WriteLine("Invalid input.");
                ExitFlag = true;
                break;
        }
    }

Note How It Works:

Let the ExitFlag be a Boolean value that controls the while loop( stop iteration and exit the while when ExitFlag is false). and are initialized with true. in each time the control enters into the while the flag is set to false so that we can avoid setting it false in multiple cases. The flag will set to true only when the default case is executed(ie., the invalid output) hence the loop repeats in this condition only.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
-1

You can use the Goto: label option in C#.

Ref: https://msdn.microsoft.com/en-us/library/13940fs2.aspx

    ReadAgain:
    string str = Console.ReadLine(); 
    char option = char.Parse(str);

    //The switch itself:
    switch (option)
    {
        case 'N':
            Console.WriteLine("Creating New App...");
            break;
        case 'L':
            Console.WriteLine("Loading App...");
            break;
        case 'O':
            Console.WriteLine("Loading Options...");
            break;
        case 'Q':
            Console.WriteLine("Exiting Application...");
            System.Environment.Exit(1);
            break;
        default:
            Console.WriteLine("Invalid input.");
            goto ReadAgain;
            break;
    }
    Console.ReadLine();
Vinothkumar
  • 235
  • 1
  • 7
  • Never use `Goto` when you can use a loop ;). – shA.t Feb 23 '16 at 06:03
  • Can you explain us why Goto is not preferred over a loop? – Vinothkumar Feb 23 '16 at 06:08
  • In your referred SO discussion, the case was closed as there is no concluded answer because this varies based on opinion and situation. Also the accepted and most rated answer in that page says that Goto is preferred in some cases to get out of deeply nested loops. So based on the situation Goto can also be preferred instead of a loop. In this example I do not understand why I cannot use Goto! Anyway since it is not related to the question of the owner of this case, I let it go. – Vinothkumar Feb 23 '16 at 07:08
  • How *Goto is preferred in some cases to get out of deeply nested loops* gives a result of *Goto can also be preferred instead of a loop* ???!!! - You can also see that the title of question that make sense enough. In general using `Goto` is a really bad choice instead of emergency cases as described in that SO link answers ;). – shA.t Feb 23 '16 at 07:56