Something wrong with code, need help

There's something wrong in the code that makes it print stuff from bottom to top instead of top to bottom but I don't know what:

#include
using namespace std;

class intstack
{
public:
int pop();
void push(int v);
bool empty();
void printstack();
intstack();

private:
int st[100];
int top;
};

intstack::intstack()
{
top = -1;
}

int intstack::pop()
{
int tmp = st[top];
top --;
return tmp;
}

void intstack::push(int v)
{
top ++;
st[top] = v;
}

bool intstack::empty()
{
return top < 0;
}

int main()
{
intstack is;
is.push(51);
is.push(132);
is.push(13);
while(! is.empty())
cout<<is.pop()<<endl;
}

void intstack::printstack()
{
for(int i = top; i >= 0; i--)
{
cout << st[i] << endl;
}
}

Comments

  • Don't know if this will work or not. but just reverse the order in which the items are printed.

    [code]
    :
    : void intstack::printstack()
    : {
    : for(int i = 0; i <= top; i++)
    : {
    : cout << st[i] << endl;
    : }
    : }
    :
    [/code]
  • : Don't know if this will work or not. but just reverse the order in which the items are printed.
    :
    : [code]
    : :
    : : void intstack::printstack()
    : : {
    : : for(int i = 0; i <= top; i++)
    : : {
    : : cout << st[i] << endl;
    : : }
    : : }
    : :
    : [/code]
    :

    Sorry man, but that didn't work
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories