how to return in main menu from sub menu

hii i need help.. when im in stack or queue menu when i choose exit i need to go back to my main menu without using a goto statements and in a new screen.

i have a missing code in stack case 7 and queue case 6 in order for it to return in 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 int main (){

    int m_menu,s_menu,q_menu,userInput,searchThis;
    Stack classStack;
    Queue classQueue;
 {
    system("cls");
    cout<<"\n*************************************";
     cout<<"\n\t MAIN MENU\n";
     cout<<"*************************************";
     cout<<"\n\n\t (1) STACK \n\t (2) QUEUE \n\t (3) QUIT \n\n";
    cout<<"Enter your choice:\t";
    cin>>m_menu;
    }

  if (m_menu==1){

    system("cls");
    while (1) {
        cout<<"\n*************************************"<<endl;
        cout<<"\t STACK MENU\n";
        cout<<"*************************************"<<endl;
        cout<<"\n\t (1) Add \n\t (2) Delete \n\t (3) Sort \n\t (4) Top \n\t (5) Display \n\t (6) Search \n\t (7) Exit \n\n";
        cout<<"Enter your choice: \t";
        cin>>s_menu;
        cout<<"-------------------------------------"<<endl<<endl;

     switch (s_menu) {
        case 1:
            cout<<"\n\n\t What do you want to put in your stack? \t";
            cin>>userInput;
            classStack.addStack(userInput);
            break;
        case 2: cout<<"\n\n\t Your deleted value is: \t"<<classStack.deleteTop()<<endl; break;
        case 3: classStack.sortingStack();  break;
        case 4: classStack.topStack();  break;
        case 5: classStack.displayStack();  break;
        case 6:
            cout<<"\n\n\t What are you searching for in your stack?\t";
            cin>>searchThis;
            classStack.searchingStack(searchThis);
            break;
        case 7:
            
        default: cout<<"\n\n\t INVALID INPUT NUMBER!"<<endl;
            } }      }
            
  else if(m_menu==2){
       
    system("cls");
    while (1) {
        cout<<"\n*************************************";
        cout<<"\n\t QUEUE MENU\n";
        cout<<"*************************************";
        cout<<"\n\n\t (1) Add \n\t (2) Delete \n\t (3) Sort \n\t (4) Display \n\t (5) Search \n\t (6) Exit \n\n";
        cout<<"Enter your choice: \t";
        cin>>q_menu;
        cout<<"-------------------------------------";

     switch (q_menu) {
        case 1 :
            cout<<"\n\n\t ADD\n\n\t What do you want to put in your queue? \t";
            cin>>userInput;
            classQueue.addQueue(userInput);
            break;
        case 2:
            cout<<"\n\n\t DELETE \n\t\t\t\t";
            classQueue.deleteFront();
            break;
        case 3:
            cout<<"\n\n\t SORT \n";
            classQueue.sortingQueue();
            break;
        case 4:
            cout<<"\n\n\t DISPLAY \n";
            classQueue.displayQueue();
            break;
        case 5:
            cout<<"\n\n\t SEARCH \n\n\t What are you searching for in your queue?\t";
            cin>>searchThis;
            classQueue.searchingQueue(searchThis);
            break;
        case 6:
            
        default: cout<<"\n\n\tInvalid input number!\n";
         } } }
   else if (s_menu=3){
       return 0;
   }
  else {
        cout<<"\n\n\tInvalid input number!\n"; }

}    
Last edited on
Break things up into much smaller functions.

You need to add your own doQueue and fix some variables, but still.
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>

int showMainMenu() {
  int m_menu;
  cout<<"\n*************************************";
    cout<<"\n\t MAIN MENU\n";
    cout<<"*************************************";
    cout<<"\n\n\t (1) STACK \n\t (2) QUEUE \n\t (3) QUIT \n\n";
  cout<<"Enter your choice:\t";
  cin>>m_menu;
  return m_menu;
}

int showStackMenu() {
  int s_menu;
  cout<<"\n*************************************"<<endl;
  cout<<"\t STACK MENU\n";
  cout<<"*************************************"<<endl;
  cout<<"\n\t (1) Add \n\t (2) Delete \n\t (3) Sort \n\t (4) Top \n\t (5) Display \n\t (6) Search \n\t (7) Exit \n\n";
  cout<<"Enter your choice: \t";
  cin>>s_menu;
  return s_menu;
}

void doStack() {
  bool done = false;
  while ( !done ) {
    int s_menu = showStackMenu();
    switch (s_menu) {
        case 1:
            cout<<"\n\n\t What do you want to put in your stack? \t";
            cin>>userInput;
            classStack.addStack(userInput);
            break;
        case 2: cout<<"\n\n\t Your deleted value is: \t"<<classStack.deleteTop()<<endl; break;
        case 3: classStack.sortingStack();  break;
        case 4: classStack.topStack();  break;
        case 5: classStack.displayStack();  break;
        case 6:
            cout<<"\n\n\t What are you searching for in your stack?\t";
            cin>>searchThis;
            classStack.searchingStack(searchThis);
            break;
        case 7:
            done = true;
            break;
    }
  }
}

int main ( ) {
  bool done = false;
  while ( !done ) {
    int m_menu = showMainMenu();
    switch (m_menu) {
      case 1:
        doStack();
        break;
      case 3:
        done = true;
        break;
    }
  }
}
thank you it is now going back to the main menu however submenus are not showing in loop
sorry im still new in programming
You need to post your latest code.
Also, the code I posted was an illustration, not necessarily a copy/paste working example.
i tried your code and did some alterations that you've said and when i run the program it doesn't go on loops like how i wanted to. and every choice i chose inside the stack and queue menu it just clears screen and doesn't show their output.


int showMainMenu() {
  int m_menu;
  system ("cls");
  cout<<"\n*************************************";
    cout<<"\n\t MAIN MENU\n";
    cout<<"*************************************";
    cout<<"\n\n\t (1) STACK \n\t (2) QUEUE \n\t (3) QUIT \n\n";
  cout<<"Enter your choice:\t";
  cin>>m_menu;
  return m_menu;
}
int showStackMenu() {
  int s_menu;
  system ("cls");
  while (1){
  cout<<"\n*************************************"<<endl;
  cout<<"\t STACK MENU\n";
  cout<<"*************************************"<<endl;
  cout<<"\n\t (1) Add \n\t (2) Delete \n\t (3) Sort \n\t (4) Top \n\t (5) Display \n\t (6) Search \n\t (7) Exit \n\n";
  cout<<"Enter your choice: \t";
  cin>>s_menu;
  cout<<"-------------------------------------";
  return s_menu; }
}
void doStack() {
    int userInput,searchThis;
    Stack classStack;
  bool done = false;
  while ( !done ) {
    int s_menu = showStackMenu();
    switch (s_menu) {
        case 1:
            cout<<"\n\n\t What do you want to put in your stack? \t";
            cin>>userInput;
            classStack.addStack(userInput);
            break;
        case 2: cout<<"\n\n\t Your deleted value is: \t"<<classStack.deleteTop()<<endl; break;
        case 3: classStack.sortingStack();  break;
        case 4: classStack.topStack();  break;
        case 5: classStack.displayStack();  break;
        case 6:
            cout<<"\n\n\t What are you searching for in your stack?\t";
            cin>>searchThis;
            classStack.searchingStack(searchThis);
            break;
        case 7: done = true;    break;
         default: cout<<"\n\n\tInvalid input number!\n";
    }
  }
}
int showQueueMenu() {
    int q_menu;
    system("cls");
        cout<<"\n*************************************";
        cout<<"\n\t QUEUE MENU\n";
        cout<<"*************************************";
        cout<<"\n\n\t (1) Add \n\t (2) Delete \n\t (3) Sort \n\t (4) Display \n\t (5) Search \n\t (6) Exit \n\n";
        cout<<"Enter your choice: \t";
        cin>>q_menu;
        cout<<"-------------------------------------";
        return q_menu;
}
void doQueue(){
    int userInput,searchThis;
    Queue classQueue;
    bool done = false;
  while (!done) {
    int q_menu = showQueueMenu();
    switch (q_menu) {
        case 1 :
            cout<<"\n\n\t ADD\n\n\t What do you want to put in your queue? \t";
            cin>>userInput;
            classQueue.addQueue(userInput);
            break;
        case 2:
            cout<<"\n\n\t Your deleted value is: \t"<<classQueue.deleteFront();    break;
        case 3: classQueue.sortingQueue();  break;
        case 4: classQueue.displayQueue();   break;
        case 5:
            cout<<"\n\n\t What are you searching for in your queue?\t";
            cin>>searchThis;
            classQueue.searchingQueue(searchThis);
            break;
        case 6: done = true; break;
        default: cout<<"\n\n\tInvalid input number!\n";
    }
  }
}
int main ( ) {
  bool done = false;
  while ( !done ) {
    int m_menu = showMainMenu();
    switch (m_menu) {
      case 1:   doStack();  break;
      case 2:   doQueue();  break;
      case 3:   done = true;  break;
    }
  }
}


Perhaps something like:

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
using namespace std;

struct Stack {
	void addStack(int) {}
	void sortingStack() {}
	void topStack() {}
	void displayStack() {}
	int deleteTop() { return 0; }
	void searchingStack(int) {}
};

struct Queue {
	void addQueue(int) {}
	void sortingQueue() {}
	void topQueue() {}
	void displayQueue() {}
	int deleteFront() { return 0; }
	void searchingQueue(int) {}
};


int showMainMenu() {
	int m_menu {};

	//system("cls");
	cout << "\n*************************************";
	cout << "\n\t MAIN MENU\n";
	cout << "*************************************";
	cout << "\n\n\t (1) STACK \n\t (2) QUEUE \n\t (3) QUIT \n\n";
	cout << "Enter your choice:\t";

	cin >> m_menu;
	return m_menu;
}

int showStackMenu() {
	int s_menu {};

	//system("cls");
	while (1) {
		cout << "\n*************************************" << endl;
		cout << "\t STACK MENU\n";
		cout << "*************************************" << endl;
		cout << "\n\t (1) Add \n\t (2) Delete \n\t (3) Sort \n\t (4) Top \n\t (5) Display \n\t (6) Search \n\t (7) Exit \n\n";
		cout << "Enter your choice: \t";

		cin >> s_menu;
		cout << "-------------------------------------";
		return s_menu;
	}
}

void doStack(Stack& classStack) {
	for (bool done {}; !done; ) {
		switch (showStackMenu()) {
			case 1:
			{
				int userInput {};

				cout << "\n\n\t What do you want to put in your stack? \t";
				cin >> userInput;

				classStack.addStack(userInput);
			}
				break;

			case 2:
				cout << "\n\n\t Your deleted value is: \t" << classStack.deleteTop() << '\n';
				break;

			case 3:
				classStack.sortingStack();
				break;

			case 4:
				classStack.topStack();
				break;

			case 5:
				classStack.displayStack();
				break;

			case 6:
			{
				int searchThis {};

				cout << "\n\n\t What are you searching for in your stack?\t";
				cin >> searchThis;

				classStack.searchingStack(searchThis);
			}
				break;

			case 7:
				done = true;
				break;

			default:
				cout << "\n\n\tInvalid input number!\n";
				break;
		}
	}
}

int showQueueMenu() {
	int q_menu {};

	//system("cls");
	cout << "\n*************************************";
	cout << "\n\t QUEUE MENU\n";
	cout << "*************************************";
	cout << "\n\n\t (1) Add \n\t (2) Delete \n\t (3) Sort \n\t (4) Display \n\t (5) Search \n\t (6) Exit \n\n";
	cout << "Enter your choice: \t";

	cin >> q_menu;
	cout << "-------------------------------------";
	return q_menu;
}

void doQueue(Queue& classQueue) {
	for (bool done {}; !done; ) {
		switch (showQueueMenu()) {
			case 1:
			{
				int userInput {};

				cout << "\n\n\t ADD\n\n\t What do you want to put in your queue? \t";
				cin >> userInput;
				classQueue.addQueue(userInput);
			}
				break;

			case 2:
				cout << "\n\n\t Your deleted value is: \t" << classQueue.deleteFront();
				break;

			case 3:
				classQueue.sortingQueue();
				break;

			case 4:
				classQueue.displayQueue();
				break;

			case 5:
			{
				int searchThis {};

				cout << "\n\n\t What are you searching for in your queue?\t";
				cin >> searchThis;

				classQueue.searchingQueue(searchThis);
			}
				break;

			case 6:
				done = true;
				break;

			default:
				cout << "\n\n\tInvalid input number!\n";
				break;
		}
	}
}

int main() {
	Queue classQueue;
	Stack classStack;

	for (bool done {}; !done; )
		switch (showMainMenu()) {
			case 1:   doStack(classStack);  break;
			case 2:   doQueue(classQueue);  break;
			case 3:   done = true;  break;
			default: cout << "Invalid menu choice\n"; break;
		}
}


As there's no code for classes Stack and Queue, I've just provided dummies so that the code compiles and the menu's display etc.

it goes on loop however the main menu should not appear in the same screen with stack and queue menu


edit: it is now working i just added the system ("cls") in the main program

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
	Queue classQueue;
	Stack classStack;

	for (bool done {}; !done; )
		switch (showMainMenu()) {
			case 1:  system("cls");   doStack(classStack);  break;
			case 2:  system("cls");   doQueue(classQueue);  break;
			case 3:   done = true;  break;
			default: cout << "Invalid menu choice\n"; break;
		}
}


thankyou so much for your help i really appreciate it.
Last edited on
Hello azalea520,

To start with when providing code it is best and very helpful to provide a complete program. Meaning all the header files, Code for any header files that you write and any input file if the that may be needed. An input file does not have to be large, but it does need to contain any record that is giving a problem.

Given some time you will come to understand that seeplus can not use system("cls"); and that is why his output will be different from yours.

Andy
hi Andy thank you for correcting me i understand im still new in programming so im not knowledgeable about others
Hello azalea520,

No worries. Sorry for the delay I had to go out for awhile.

I see that you have green checked the post, but if you want to go over your code click on my name and send me a Private Message that I can reply to and I will explain the problems I found in you original code.

Andy
Topic archived. No new replies allowed.