I’m trying to create the following structure for a C++ program in the terminal:
Menu:
[1] Add Number
[2] View Numbers
[3] View Average
[CHOICE 1]
- Add a Number to array
- Once finished, show Menu again so user can select another action
[CHOICE 2]
- View all numbers in array
- Once finished, show Menu again so user can select another action
[CHOICE 3]
- View average of all numbers in array
- Once finished, show Menu again so user can select another action
I’m not sure exactly how to set this up. When the user inputs each menu item’s respective number, the corresponding information appears. That’s easy enough, using if statements to look for the user’s inputted number.
However, my problem comes when I try to show the menu again so the user can select another action. I recognize some sort of loop is necessary here but I’m not sure how to go about it.
Can you help me set up a basic structure of how to start this? I’d really appreciate it.
Break down your menu entries into functions to keep everything clean and easy to grasp (for now).
This can be expanded a lot, creating some kind of menu handling etc, but I’d just stick to something like this:
As Kerrek mentioned in a comment, this doesn’t really check whether the user actually picked any integer value at all. This makes the code a bit more complicated, but in general would be good practice (as thing scan get nasty, if you ignore such things):
Replace
cin >> choice;above with this: