C++ || Help With Console Menu & Status

Hi guys, I have some questions relating to a game trainer I am making. I have tidy up the code from my old code. Also if anyone can have a look at my code and see if the bracket scopes are all in the right place and is everything looking good?

Have a few things I want to know aswell:

Question (1): Lets say the program is running I press 1, for Case 1 option. It goes into the page displaying the message. Once the message is displayed how would I make the user press enter to go back to the main menu page instead of program closing?

Question (2): How would I make the main menu have a status ON/OFF like if I press option 1,2 it will update the main menu saying status: ON or OFF?

TA!


Here is my full code: https://pastebin.com/jZ35D6c0

Last edited on
Question (1): use a loop in main like so
1
2
3
4
5
for(;;)
{
  // get input
  // process input - if end condition break to leave the loop
}
Well to make it more clear I want my program to do this:

MENU
1-Health
2-Ammo
3-Ammo 2

When 1 selected it display in new screen:

Health not available at the moment.
Press any key to continue....

After pressing any key I want it to go back to main menu.

Please have a look at my code in the link to see what I should do.



He told you what to do. You need to use a loop and put your processing inside of the loop.
Why don't you try it first?
You will learn much more in this way.
Asking other people to write the code for you is like others asking to do push-ups or diet for you.
Who will get the benefit?
Ok I put a for(;;) loops at the very top just under main() and end the for loop all the way at the end just before main() end.

But when it loops say I press key 1, it will display message. Then when press any key to continue it will loop back to main menu but it still has the previous message displayed from case 1 or has "press any key to continue" on the main page. How do I completely remove all that once I return to the main menu.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <string>

std::size_t get_choice( std::size_t maxv )
{
    std::cout << "please enter you choice: [1-" << maxv << "]: " ;

    std::size_t choice ;
    if( std::cin >> choice && choice <= maxv ) return choice ;

    std::cin.clear() ;
    std::cin.ignore( 1000, '\n' ) ;
    return get_choice(maxv) ;
}

std::size_t get_menu_selection( const std::vector<std::string>& options )
{
    std::cout << "\n--------------\n" ;
    for( std::size_t i = 0 ; i < options.size() ; ++i )
        std::cout << i+1 << ". " << options[i] << '\n' ;
    std::cout << "\n--------------\n" ;

    return get_choice( options.size() ) ;
}

void not_available_stub( const std::string& option )
{
    std::cout << '\n' << option << " not available at the moment.\npress enter to continue..." ;
    std::cin.ignore( 1000, '\n' ) ;
    std::cin.get() ;
}

int main()
{
    const std::vector<std::string> menu { "Health", "Rifle Ammo", "Pistol Ammo", "Quit" } ;

    auto sel = get_menu_selection(menu) ;
    while( sel < menu.size() )
    {
        not_available_stub( menu[sel-1] ) ;
        sel = get_menu_selection(menu) ;
    }
}
Topic archived. No new replies allowed.