How to return to int main() from other functions pressing the key ESC?

Hello there.
I want to do something very simple but probably the solution is very tricky since I am still very new to programming, I have a main menu void function which I call in Main to be printed out, then I select from that main menu the option I want and it leads me to a sub-menu, from that sub-menu which is already located in others function I want to go back to the main menu located in main by pressing Escape key at any moment while working on that function, any simple way to do so?

Just an example of my code

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void printMainMenu()
{
  code here
} 

double funtion01()
{
  //sub menu
  code here
}

double funtion02()
{
  //sub menu
  code here
}

double funtion03()
{
  //sub menu
  code here
}

int main()
{
 int option;
 printMainMenu();
 cout << " Choose an option: ";
 cin >> option;
 
 // code here
 // if press 1 go to function01() if 2 go to function02() and so on

return 0;
}
You need to run the main menu in a loop. When you end the the function end it will automatically go back to the main menu. Like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
 int option;
 bool running = true;
 
 while(running)
 {
    printMainMenu();
    cout << " Choose an option: ";
    cin >> option;
    switch(option)
    {
      // code here
      // if press 1 go to function01() if 2 go to function02() and so on
      // when exit chosen set running to false
    }
 }
 



return 0;
}


mm what about the ESC key recognition... How do I achieve that?
Escape is a key with the numerical value of 27 - if I remember correctly. So when a key is pressed you need to check if it is the Escape key and then you exit the function.
Topic archived. No new replies allowed.