Stacks

I need help with the output please. The program is good when entering the seven numbers but it dont output any number when entering the number seven number into a stack. The code below and thank you for taking your time to look at the 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
42
43
44
45
46
47
48
49
50
51
#include <iostream>

using namespace std;

class Stack
{
    int number[7];
    int top;
public:
    Stack();
    void push(int x);
    bool emptystack();
    int pop();
};
int main()
{
    Stack st;
    int num;
    for(int i = 0; i <= 6; i++)
    {
        cout << "Enter a number:";
        cin >> num;
        st.push(num);
    }
    while(st.emptystack())
    {
        st.pop();
        cout << num << st.pop() << endl;
    }
    cout << endl;
    return 0;
}
Stack::Stack()
{
    top = -1;
}
void Stack::push(int x)
{
    top++;
    number[top] = x;
}
int Stack::pop()
{
    top--;
    return number[top +1];
}
bool Stack::emptystack()
{
    if(top == 0){return 1;}
    else {return 0;}
}
first of all this code is a fuckin mess.
please put the functions above the main and make some new line between functions.
it is just so much easier if main is always the last function and you can see where something starts or ends.

also dont change your counting index (top in your case), before you have actually changed the variable.

your emptystack() function is returning 1 if the stack is empty, but your while statement in line 25 only pops and prints stuff if emptystack returns 1.

and what is line 27 for?
Last edited on
Calling the pop function st.pop();
Sorry for the messing code, is the error in the while loop if so can you give me a example please?
I thought the error is within the output section of the code within the while loop?
would the conditions have to been while (!st.emptystack()) or no?
Thank for your help!
would the conditions have to been while (!st.emptystack()) or no?

Yes.
Last edited on
Cool, I change the while loop but it output funny e.g if user input seven number 1, 3, 5, 7, 11, 13, it output
11, 5, 1
when i need it to output
13, 11, 7, 5, 3, 1
Clearly it hits every-other one,
the val top, is pointing to every-other value and this can be seen by placing ChkFlgs in each subrt'n .

top = 2, top = 4, top=6

( actually a much better name for this val would be somethine like:
int i_stk_pntr, or i_stackPointer as it describes both it's function and it''s type.

btw, why all of this code? First you subtract 1 then you add 1 ??

1
2
3
4
int Stack::pop()
{
    top--;
    return number[top +1];


So that it's ONLY function is to return what it already had. e.g. it's useless.
Last edited on
1
2
3
4
5
while(st.emptystack())
    {
        st.pop();
        cout << num << st.pop() << endl;
    }


By calling st.pop() Twice you then seem to process every other value of
the int 'top'
@Incis B Thank you very much I got it working. Thank you to very one here who left a comment to help. I am grateful for your time and help thank you very much. Incis B it was a easy mistake to fix thanks for the help Lool!!!
Last edited on
Topic archived. No new replies allowed.