Hi there.
I'm having a slight bit of trouble when using a multi level case statement in my program. I'm not sure if it's because you shouldn't use case statements inside of case statements or if it's because I haven't yet learned enough about using them.

Basically I want to have an option within each nested case statement to return to the initial menu rather than being stuck in the sub menu.

//Display initial menu
            //Read input 
            do
            {
                switch (//On input)
                {
                    //Display Sub Menu
                    case 1:
                        switch (//On input)
                        {
                            case 1:
                                break;

                            case 2:
                                break;
                        }
                        break;

                     //Display Sub Menu
                     case 2:
                         switch (//On input)
                        {
                        }
                        break;
                 }
               While(user doesn't select close from first menu)

Thanks.

Recommended Answers

All 11 Replies

Hmm, we would need a BETTER explanation of the problem here. Why would you use switch inside a switch?
Makes no sence to me...yet!

I have a main menu with the choices Dog,Cat,Turtle etc. User selects Dog, and is given new options to choose from, Add Dog, Edit Dog, Delete Dog. If a user adds a dog it returns to this sub menu, There is no way to get back to the original set of choices (cat, turtle)

(This is in console)

I'm just guessing you are using a windows forms application.
Make your menu in the designer and install Click handlers for all your menuitems(submenus of submenu etc. included)
No need for a complicated switch case senario!

No, Unfortunately this needs to be written in console.

Can you show us ur code? The part thats bothering you.
Iam defenatelly sure there is NO need to use switch inside another switch statements. I did more complicated codes, and so far never did something like you :)

Ok well I still can't work it out so I've decided to just have one big menu. I'm still having problems with the program just exiting rather than looping after each case though. My code is similar to this

DisplayMenu();
string userSelect = Console.ReadLine();
switch (userSelect)
{

 Case "1"
{
//Asks user for information and stores in an array
}
break;

Case "2":
{
//Works out an equation
}
break;

Case "3":
{
//Prints data out to screen
}
break;

}

After each case the program exits. What I want it to do is go back to the first stage where the menu is displayed and the user is able to select another option until they want to quit.

Why not using some while(true) loop?
Then then you want to exit, just do "break;".

I have a while(x!=#) but it seems to just be closing the program. I'm sure my breaks are where they should be as well :S

Aha, Ok I've managed to get it looping now. My problem was that I had the menu and switch before the loop. D'oh.

One last question relating to this. How would I prevent the user from entering nothing at the menu select causing the program to crash?

You mean, user MUST enter something?
Something like this?

string input = "";
            while (input.Length == 0)
            {
                input = Console.ReadLine();
                if (input.Length == 0)
                    Console.WriteLine("Please enter some text...");
            }
            Console.WriteLine("This is users input: {0}.", input);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.