Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stack question

Reply
Thread Tools

stack question

 
 
pentiumPunk
Guest
Posts: n/a
 
      08-31-2003
string something;

while(condition)
{
cin>> something;
node<T> *x;
x = new node<T> (something);
aStack.push(x);
}

i want to keep adding "something" to a stack, and top( ) and pop( ) it off,
but the problem is...... x is being overridden each time. how am i going to
manage the nodes in the stack ? thanks!





 
Reply With Quote
 
 
 
 
Kristofer Pettijohn
Guest
Posts: n/a
 
      09-01-2003
pentiumPunk <(E-Mail Removed)> wrote:
> string something;
>
> while(condition)
> {
> cin>> something;
> node<T> *x;
> x = new node<T> (something);
> aStack.push(x);
> }
>
> i want to keep adding "something" to a stack, and top( ) and pop( ) it off,
> but the problem is...... x is being overridden each time. how am i going to
> manage the nodes in the stack ? thanks!


If what you want to keep adding to a stack is the variable 'something',
you may want to read up on containers... Try...

string something
stack<string> aStack; // a stack of strings

while (condition) {
cin >> something;
aStack.push(something);
}

Kristofer

 
Reply With Quote
 
 
 
 
llewelly
Guest
Posts: n/a
 
      09-01-2003
"pentiumPunk" <(E-Mail Removed)> writes:

> string something;
>
> while(condition)
> {
> cin>> something;
> node<T> *x;
> x = new node<T> (something);
> aStack.push(x);
> }
>
> i want to keep adding "something" to a stack, and top( ) and pop( ) it off,
> but the problem is...... x is being overridden each time. how am i going to
> manage the nodes in the stack ? thanks!


Maybe this:

#include<string>
#include<vector>
#include<stack>
#include<iostream>
#include<ostream>

using namespace std;

int main()
{
string something;
stack<string> astack;
while(cin >> something)
{
astack.push(something);
}

cout << "\nDumping Stack." << endl;
while(astack.size())
{
cout << astack.size() - 1 << " : " << astack.top() << endl;
astack.pop();
}
}

is helpful?

If not, please explain the special features of your stack.
 
Reply With Quote
 
pentiumPunk
Guest
Posts: n/a
 
      09-01-2003
this was very helpful. thanks!



 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-01-2003
pentiumPunk wrote:

> string something;
>
> while(condition)
> {
> cin>> something;
> node<T> *x;
> x = new node<T> (something);
> aStack.push(x);
> }
>
> i want to keep adding "something" to a stack, and top( ) and pop( ) it off,
> but the problem is...... x is being overridden each time.


"overridden"? How so? I know of nothing in C++ other than virtual
functions that can be overridden, and I don't see that happening here.

Perhaps you meant "overwritten"? If that's the case, I would agree that
x is being overwritten each time, but I would disagree that this is a
problem. The value of x ceases to be important once you've pushed it
onto the stack.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
Why stack overflow with such a small stack? Kenneth McDonald Ruby 7 09-01-2007 04:21 AM
"stack level too deep"... because Threads keep their "starting" stack Sam Roberts Ruby 1 02-11-2005 04:25 AM



Advertisments