Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 347875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:18:44+00:00 2026-05-12T11:18:44+00:00

Hey all! Having a little trouble with my stack. Im trying to print each

  • 0

Hey all! Having a little trouble with my stack. Im trying to print each element that I’ve pushed onto the stack.

Starting with the stack ctor we know that we have a fixed size for the array. So I allocate the items struct object to hold just that much space:

stack::stack(int capacity)
{   
  items = new item[capacity];

  if ( items == NULL ) {
    throw "Cannot Allocoate Sufficient Memmory";
    exit(1); 
  }
  maxSize = capacity;
  top       = -1;
}

Yes, items is a struct type of the object “item”. Have a look:

class stack
{
  stack(int capacity);
  ~stack(void);
...
  private:
    int maxSize; // is for the item stack
    int top;     // is the top of the stack
    struct item {
      int n;
    };
    item        *items;                 

  public:
    friend ostream& operator<<(ostream& out, stack& q)
  ...

First and formost we want to add to the stack by pushing each incoming element into the array FILO:

bool stack::pushFront( const int n )
{
     if ( top >= maxSize-1 ) 
{
    throw "Stack Full On Push";
    return false;
}
else 
{
    ++top;
    items[top].n = n;
}
return true;
}

// just a textbook example here:
stack::~stack(void)
{
  delete [] items;

  items    = NULL;
  maxSize = 0;
  top      = -1;
}

Yes the real issue for me is the items[++top].n = n; statement. I’ve been trying to find out how I can drag (+) the items array out to see ALL of the array elements after I push onto the stack.

Im wondering why I cant drag that items[++top].n = n statement out when im debugging. All that comes up is the value that is passed as an ‘n’ paramater. Do I need to use a stack object type array to store the values into?

When I overload the << operator and try to print the elements I get an insanely large negative number:

ostream& operator<<(ostream& out, stack& q)
{
    if ( q.top <= 0 ) // bad check for empty or full node
    out << endl << "stack: empty" << endl << endl;
    else
        for ( int x = 0; x < q.maxSize; x++ )
        {
            out << q.items[x].n; // try to print elements
        }
    return out;
}

I’m way off and I need some guidence if anyone has the time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:18:45+00:00Added an answer on May 12, 2026 at 11:18 am

    In the overloaded << operator in the for loop you are iterating maxsize times. But you might not have pushed maxsize elements into the stack. You should iterate top times. Also, write a default constructor for item structure and initialize all the variblaes so that you do not get garbage values when you try to print them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, I'm trying to write a sort function but am having trouble figuring
Hey all, I'm having a little trouble packing a numpy.float32 using the struct.pack function.
Hey all, i'm having a little problem with ksoap2 and getting response i have
Hey all, i am trying to split a users name but i am having
Hey all, would appreciate any help with a little problem im having. Basically my
Hey all, I'm having some trouble setting up my grails application. Running the app
Hey all, I'm having trouble with PerfMon on one system out of fifteen in
Hey all. I'm having some trouble getting punctuation to be stripped out of an
Hey all, I'm trying to fix up my numerous scenarios that test out pages
Hey everyone, i am having trouble finding a memory leak. all off my retain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.