Inspect the Top Element of a Stack

My code for inspecting the top element on the stack has flaws but I prefer not to use the STL implementation for educational purposes. Is there any other way to find the top element on the stack, which is private, so I can compare it to the colorwanted????

in main()
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
#include <iostream>
#include "Stack.h"
//#include <stack>

using namespace std;


class Gumball {
    public:
        string color;
        int counter;
};

typedef Gumball Type;

int main()
{
    Stack s, gumballStack;
    Gumball g;
    g.counter = 0;
    int topElement;
    string wantedcolor;
    char choice;
    bool choice_flag = true;

    do {
        cin >> choice;
        cin >> g.color;
        switch(choice)
        {
            case 'b':
            case 'B':
                cout << "A" << " " << g.color << " gumball has been bought." << endl << endl;
                s.isempty();
                s.push(1);
                if(!s.isfull())
                    cout << "The gumball is" << " " << g.color << " and has been stored." << endl << endl;
                else
                    cout << "There is no room for another gumball." << endl << endl;
                break;
            case 'e':
            case 'E':
                s.isempty();
                wantedcolor = g.color;
                topElement = s.pop();
                while(!s.isempty() && wantedcolor != topElement) //ERROR!!
                      {
                        gumballStack.push(topElement);
                        g.counter++;
                      }
                int i;
                for(i = 5; i >= 0; i--)
                    gumballStack.push(i);
                for(int i = 0; i < 5; i++)
                    if(topElement == g.color){
                        s.pop();
                        g.counter++;
                        cout << "A" << " " << g.color << " gumball has been eaten." << endl << endl; }
                if(s.isempty())
                    cout << "The original stack is now empty," << endl;
                for(int i = 0; i <= 5; i++){
                    gumballStack.pop();
                    s.push(i); }
                if(gumballStack.isempty())
                    cout << "The holding stack is now empty." << endl;

                break;
            case 'q':
            case 'Q':
                choice_flag = false;
                break;
        }
    } while(choice_flag);

    return 0;
}


.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef STACK_H
#define STACK_H


// Interface file  - Stack class definition
class Stack {
    public:
        Stack();
        void push(int);
        int pop();
        bool isempty();
        bool isfull();
        Stack *p;
    private:
        int gumballs[6+1];
        int top;
};


#endif // STACK_H 
closed account (D80DSL3A)
You have a pop(). If you want to get the value without removing it from the stack then add a peek().
Ok, I'll try this.
That may not work since im not reading in from a file
Topic archived. No new replies allowed.