Home > Mobile >  How do I add a Y/N to Switch-Cases in C#?
How do I add a Y/N to Switch-Cases in C#?

Time:10-12

I'm a complete Newbie to the world of programming, yet I really wish to learn a lot as quick as possible and now came up to a problem that I can't find to solute just via researching and "learning- by doing" (Trying around).

Basically I'm trying to work on a small console- based TextAdventure in C Sharp (With VisualStudios) Now I came to a Case- Switch (Offering the User some Options to read and walk through), but I wish to add a Y/N Confirmation in case the User decides to take a different path. For now it's only for the starting point of the story: Does the User want to go into "The Wilds", "The City", "The Farm". Something as simple as that just in addition: "Are you sure (Y/N)?" leading the No to return the given choices. Thank you all in advance and stay healthy!

Menu mainMenu = new Menu(prompt, options);
        int selectedIndex = mainMenu.Run();

        switch (selectedIndex)
        {
            case 0:
                EnterTheWorld();
                break;
            case 1:
                VisitTheMemorial();
                break;
            case 2:
                TakeYourLeave();
                break;
            default:
                break;
        }
    }

               
            private void TakeYourLeave()
    {
        WriteLine("\nYou are about to take your leave... Are you sure ? (Y/N)");

        ReadKey();
        Environment.Exit(0);
    }
    private void VisitTheMemorial()
    {
        Clear();
        //FILLER//
        WriteLine("You may proceed by the press of any button.");
        ReadKey(true);
        RunMainMenu();
    }
    private void EnterTheWorld()
    {

        string prompt = "Where would you like to start your journey?";
        string[] options = { "The Slums", "The Wilds", "The City", "The Farm" };
        Menu startMenu = new Menu(prompt, options);
        int selectedIndex = startMenu.Run();
       
      
        BackgroundColor = ConsoleColor.White;
        switch (selectedIndex)
        {
            case 0:
                ForegroundColor = ConsoleColor.Black;

                WriteLine("\n ||Small Description||Are you sure to take this path? (Y/N)");
              


                break;
            case 1:
                ForegroundColor = ConsoleColor.Green;
                WriteLine("\n ||Small Description||Are you sure to take this path? (Y/N)");


                break;
            case 2:
                ForegroundColor = ConsoleColor.Red;
                WriteLine("\n ||Small Description||Are you sure to take this path? (Y/N)");


                break;
            case 3:
                ForegroundColor = ConsoleColor.Yellow;
                WriteLine("\n ||Small Description|| Are you sure to take this path? (Y/N)");



                break;
        }

CodePudding user response:

In this case, you need to take care of a few things:

  1. Make sure that the entire switch-statement logic is in a while-loop, as such:
while (True) {
        int selectedIndex = mainMenu.Run();

        switch (selectedIndex)
        {
            case 0:
                EnterTheWorld();
                break;
            case 1:
                VisitTheMemorial();
                break;
            case 2:
                TakeYourLeave();
                break;
            default:
                break;
        }
}
  1. Check with your function if the input given is "No", if such, then immeditely return so your function exits before any navigation
private void VisitTheMemorial()
{
   userInput = readKeyFunction();
   if (userInput == "no") {
      return;
}}
  1. Finally, if the user doesn't select no, just do nothing and let the function go on

CodePudding user response:

It is best to handle the 'Are you sure?' inside the menu.Run(), and return a value to the selectedIndex after the confirmation. Also avoid using while loop at the main flow in this case.

Note: you have to think in a modular way. Consider your Menu class as a module which handles user choice. The main flow does not have to bother about the confirmation by the user, it just have to process the final result of the user.

Another suggestion: Use enums instead of integers wherever possible for multiple choices.

  •  Tags:  
  • c#
  • Related