I'm trying to just use a normal stack and have it pop the numbers in the order stacks normally go but it just keeps popping 28 (or at least from output thats what i see) it should just be pretty straight forward but ....

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    stack<int> number;
    int x=0;
    number.push(43);
    number.push(34);
    number.push(45);
    number.push(28);
    x=number.top();
 while(!number.empty()){
        number.pop();
        cout<<x;
    }
return 0;
}

Recommended Answers

All 9 Replies

You are not resetting x to number.top after the stack is modified. So what is happening is you are setting x to 28, then in the loop you are modifying the stack but simply outputting 28 over and over! You need to add a x=number.top() between the number.pop() and the cout<<x.

Dave

wow that was fast thanks

under windows it causes it to crash though any suggestions ?
since x is a static variable reassigning it is causing it to crash right?

No no, it is definitely ok to reset that variable. The problem is that you have popped the whole stack, then you are trying to get the top() of the stack, which is not defined.

This code runs fine

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    stack<int> number;
    int x=0;
    number.push(43);
    number.push(34);
    number.push(45);
    number.push(28);
    x=number.top();

 while(number.size() > 1)
{
        number.pop();
	x=number.top();
        cout<<x << std::endl;
    }
return 0;
}

I'll leave it up to you to figure out the logic to get all 4 items, since this only gets 3 :)

Good luck,

Dave

ok thanks for the help

after cout<<x; increment x so it points to next value
like

cout<<x;
x++;
}

try this it might work

No no, you can't increment x like that, it will just turn 28 into 29!

No no, you can't increment x like that, it will just turn 28 into 29!

i thought u r using 'x' as the index for your array

i thought u r using 'x' as the index for your array

no i am using x to output whats on top of the stack. ;)

number.push(43); //puts 43 on top of the stack
number.pop(); //removes the top of the stack
cout<<x; //for printing the the top of the stack

basically a stack is like well a stack. imagine you have 4 phone books neatly placed on top of each other then you put another phone book on top (that is known as pushing) and then you take one off the top (that is known as popping). you cant take from the bottom or the middle however. first in last out

example:
input: 1
input: 2
input: 3

output:3
output:2
output:1

btw here is the finished code if anyone would like to see where i messed up.
thanks david :)

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    stack<int> number;
    int x=0;
    number.push(43);
    number.push(34);
    number.push(45);
    number.push(28);
 while(number.size()>0){
        x=number.top();
        cout<<x<<endl;
        number.pop();
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.