Monday, May 24, 2010

In c++ ? need help?

i pushed a string to the stack when i pop it, it pops another value when its a number why?


slstack%26lt;int%26gt; st;


string s;


getline(cin,s);


long u=s.length();


for( int i=0; i%26lt;u; i++)





{ int x2=st.popitem(); cout%26lt;%26lt;"x2"%26lt;%26lt;x2%26lt;%26lt;endl;


int x1=st.popitem(); cout%26lt;%26lt;"x1"%26lt;%26lt;x1%26lt;%26lt;endl;

In c++ ? need help?
Even if you do push() where you are suppose to do so, you left out the most important part of the code - the stack. I assume the point of the exercise is to write the stack code so there is no way to tell what is wrong.





Post the stack code.





Just a wild guess but if you are pushing chars from the string onto the stack, maybe you are seeing the ascii values for the letters, spaces, etc.?
Reply:The previous answerer is right in that I don't see a push there. But, stacks are FIFO structures (First in, First Out)
Reply:I don't see where you pushed the string onto the stack...also, when you push anything onto a stack, when you pop something off the stack, everything is reversed order. It's the "first one in, last one out" rule.


No comments:

Post a Comment