empty the value inside an array

hi i have my main menu and sub menus each of it appears in new screen my problem is when im in stack menu for example i input an element then choose exit my program will return to the main menu however when i choose again the stack menu the elements inside it are still there. it should be empty again every time i choose exit.
my variable is an array.

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

  #include <cstdlib>
 #include <iostream>
using namespace std;

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

class 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;
	while (1) {
		cout << "\n*************************************" << endl;
		cout << "\t STACK MENU"<<endl;
		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"<<endl;
		cout << "Enter your choice: \t";
		cin >> s_menu;
		cout << "-------------------------------------"<<endl;
		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";
		}
	}
}

int showQueueMenu() {
	int q_menu {};

	cout << "\n*************************************"<<endl;
	cout << "\t QUEUE MENU"<<endl;
	cout << "*************************************"<<endl;
	cout << "\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"<<endl;
	cout << "Enter your choice: \t";
	cin >> q_menu;
	cout << "-------------------------------------"<<endl;
	return q_menu;
}

void doQueue(Queue& classQueue) {
	for (bool done; !done; ) {
		switch (showQueueMenu()) {
			case 1:{
				int userInput;
				cout << "\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";
		}
	}
}
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;
		}
}
Last edited on
so, your stack should only live inside `doStack()' and every time that function is called you want it to reset
perhaps you should declare it in that function.

also, your testcase does not show your issue.
it should be empty again every time i choose exit.


That was not the way I coded the above. As you didn't say originally this was required, I coded it so that when entering/leaving the stack/queue menus the previous entries were still present - as this seemed logical!
hi ne555 ow sorry ill edit it and put all of my codes


hi seepluss im sorry for that i did these two separately at first then i merge it that is why i didnt know it will have that kind of error.
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <cstdlib>
#include <iostream>
using namespace std;

class Stack{
    private:
        int stackHolder[25],topValue = -1;
    public:
        Stack (){
            topValue = -1;
            for (int v=0; v<25; v++)
            {
                stackHolder[v] = 0;
            } }
    bool isEmpty(){
        if (topValue == -1)
            return true;
        else
            return false; }

    bool isFull(){
         if (topValue == 24)
            return true;
         else
            return false;  }
//adding stack
    void addStack (int addtoStack){
        if (isFull()) {
            cout<<"\n\n\t FULL! CANNOT ADD MORE VALUE"<<endl;
        }
        else {
            topValue++;
            stackHolder[topValue]=addtoStack;  } }
//deleting stack
    int deleteTop(){
        if (isEmpty()) {
            cout<<"\n\n\t EMPTY! ADD SOME VALUE"<<endl;
            return 0; }
        else{
        int deletedValue = stackHolder[topValue];
            stackHolder[topValue] = 0;
            topValue--;
            return deletedValue; }  }
//sorting stack
    void sortingStack(){
        int outer,inner,storage;
            if (isEmpty()) {
                cout<<"\n\t NO VALUE TO SORT!"<<endl;
            }
            else {
                for (outer=0; outer<topValue; outer++)
                {
                    for (inner=0; inner<(topValue-outer-1); inner++)
                    {
                        if(stackHolder[inner]>stackHolder[inner+1])
                        {
                        storage =stackHolder[inner];
                        stackHolder[inner] = stackHolder[inner+1];
                        stackHolder[inner+1] = storage;
                        }
                    }
                }
                cout<<"\n\t Your sorted number:  "<<endl;
                for (outer=0; outer<=topValue; outer++)
                {
                    cout<<"\t\t"<<stackHolder[outer]<<"\t"; } } }
//showing top stack
    void topStack (){
        if (isEmpty())
            cout<<"\n\t NO VALUE INSIDE!"<<endl;
        else
            cout<<"\n\t The top value is \t"<<stackHolder[topValue]<<endl;  }

    void displayStack(){
        if (isEmpty())
            cout<<"\n\t NO VALUE TO DISPLAY!"<<endl;
        else
            cout<<"\n\t Elements inside the stack:  "<<endl;
            for (int disp=topValue; disp>=0; disp--)
            cout<<stackHolder[disp]<<" ";
            cout<<endl;   }
//searching stack
    void searchingStack(int findThis) {
        int found,address;
            if (isEmpty()) {
                cout<<"\n\n\t EMPTY! NO VALUE TO SEARCH"<<endl; }
            else {
                    found=-1;
                    for (int srch=0; srch<25; srch++)
                    {
                        if (findThis == stackHolder[srch])
                    {
                        found =1;
                        address=srch;
                    }
                    }
                if (found ==-1)
                    cout<<"\n\t" <<findThis<< " was not found in the stack "<<endl;
                else
                    cout<<"\n\t" << findThis<< " is found at " << address+1<< " address in the stack"<<endl; } }
            };

class Queue{
    private:
        int queueHolder[25];
        int firstIn, lastIn;
    public:
       Queue(){
       firstIn =-1;
       lastIn = -1;
       for(int i=0; i<25;i++)
       {
        queueHolder[i]=0;
       } }

    bool isEmpty(){
        if(firstIn == -1 || firstIn>lastIn)
            return true;
        else
            return false;
        }

    bool isFull(){
        if(lastIn == 24)
            return true;
        else
            return false;
        }
//add queue
    void addQueue(int addtoQueue){
        if(isFull()){
            cout<<"\n\t\t FULL! CANNOT ADD MORE VALUE\n";
            return;
        }
        else{
            if(firstIn==-1)
            firstIn=0;
            lastIn++;
            queueHolder[lastIn]=addtoQueue;
            } }
//delete queue
    int deleteFront(){
        if (isEmpty()) {
            cout<<"\n\t\t EMPTY! ADD SOME VALUE \n";
            return 0; }
        else{
            int deletedValue = queueHolder[firstIn];
            queueHolder[firstIn] = 0;
            firstIn++;
            return deletedValue;
            }  }
//sorting queue
    void sortingQueue(){
            int outer,inner,storage;
                if (isEmpty()) {
                    cout<<"\n\t NO VALUE TO SORT!"<<endl;
                    }
                else {
                    for (outer=0; outer<lastIn; outer++)
                    {
                        for (inner=0; inner<(lastIn-firstIn); inner++)
                        {
                            if(queueHolder[inner]>queueHolder[inner+1])
                            {
                            storage =queueHolder[inner];
                            queueHolder[inner] = queueHolder[inner+1];
                            queueHolder[inner+1] = storage;
                            }
                        }
                    }
                    cout<<"\n\t Your sorted numbers: ";
                    for (outer=0; outer<=lastIn; outer++)
                    {
                        cout<<queueHolder[outer]<<" "; } }
                    }
//display queue
    void displayQueue(){
        if (isEmpty()) {
            cout<<"\n\t\t NO VALUE TO DISPLAY!\n";
        }
        else{
            cout<<"\nElements in queue: ";
            for(int i=firstIn; i<=lastIn; i++)
            cout<<queueHolder[i]<<" ";
            cout<<endl;
        }  }
//search queue
    void searchingQueue(int findThis){
        int found,address;
            if (isEmpty()) {
                cout<<"\n\t EMPTY! NO VALUE TO SEARCH\n"; }
            else {
                    found=-1;
                    for (int srch=0; srch<25; srch++)
                    {
                        if (findThis == queueHolder[srch])
                        {
                            found =1;
                            address=srch;
                        }
                    }
                if (found ==-1)
                    cout<<"\n\t" <<findThis<< " was not found in the queue "<<endl;
                else
                    cout<<"\n\t" << findThis<< " is found at " << address+1<< " address in the queue "<<endl;
                    } }
};


here it is i cant add this above because its says that it is too long
hi seepluss im sorry for that i did these two separately at first then i merge it that is why i didnt know it will have that kind of error.


IMO, I'd keep it the way it is and add a member function to the Stack and Queue classes to clear the data (reset back to empty). Then have an extra menu option to do so.
Topic archived. No new replies allowed.