-1

First off, new to c++.

Background: I am using a series of switches for a simple console menu like this:

...
Switch (user_input)

{

     case 1:

          Submenu1();

          Break;

      ...

The submenu1 function lives in a different .cpp file. The submenus each have a similar case to get back to the main menu, and other cases for utility programs, manipulating large data files. The solution compiles and I can navigate back and forth from the menu to the submenus and back.

Questions: Each time I make a new menu selection, am I correct in thinking I am burrowing into lays of these switches? It doesn't seem like I'll ever get to the break statements since the console is still running the next function. Will this ever impact anything? Independent of the specific performance of this instance, I'm looking for guidance on the best practices and principles and not just fudging it.

Thanks!

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
rumble_bumbles
  • 136
  • 1
  • 6
  • "*I'm looking for guidance on the best practices and principles*" this question may be better suited for [SoftwareEngineering.se](https://softwareengineering.stackexchange.com/help/on-topic) – scohe001 Feb 07 '18 at 21:53
  • Well you will reach the break statement once your program accepts an exit command of some sort (Unless you do something like `exit()` which is not recommended). No matter how much you burrow, once the function most recently called ends, each of the functions called before will also end. The only way you will never reach the break is if you `exit` or if your program just keeps going deeper and deeper, never stopping. – Arnav Borborah Feb 07 '18 at 22:05
  • Yes, you are destined for maintenance hell, unless you consider placing the data into a table and using a table driven system. In a table driven system, you could add or remove rows without changing the code (engine). – Thomas Matthews Feb 07 '18 at 22:05
  • Read about _fallthrough_ (reaching the next case label without a break) [here](http://en.cppreference.com/w/cpp/language/switch) – Mohammad Kanan Feb 07 '18 at 22:22

1 Answers1

0

I am burrowing into lays of these switches?

Yes, and this is bad.

Will this ever impact anything?

Yes, it will make your program unmanageable.

You absolutely need to find a way for your submenu to return, once they're done. Build you program in a hierarchic way. Menu into submenu, then return back to main menu.

If your flow becomes more modular, then you should have a

Switch (user_input)
{
 case 1:
      nextMenu = Submenu1;
      Break;
...
}

// later on, in a way that doesn't nest

nextMenu.display();

Where your menus are classes, or functors, or something. But for now, find a way to use return;

Jeffrey
  • 11,063
  • 1
  • 21
  • 42