Jump to content

inheritance, push(), pop(), it doesn't run

- - - - -

  • Please log in to reply
3 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

What is wrong with this code? It's not running. It compiles okay but when I run it, it simply says, "Press any key to continue . . .". Please help me with it. Thank you.


#include <iostream>

using namespace std;

#include <cstdlib>

#include <string>


const int MAX = 10;

////////////////////////////////////////////////////////////////

class Stack

{

        protected:

            //static const int MAX=10;

            int st[MAX];

            int top;

        public:

            Stack(): top(-1)

                {/*empty body*/}

        void push(int var)

                { st[++top] = var; }

        int pop()

                { return st[top--]; }

};

////////////////////////////////////////////////////////////////


class Stack2 : public Stack

{

        public:

            void push(int var)

                {

                    if(top >= MAX-1) // (MAX-1) because array starts from 0

                        { cout << "\nError: stack is full"; exit(1); }


                    Stack::push(var);

                }


            int pop()

                {

                    if(top < 0)

                        { cout << "\nError: stack is empty\n"; exit(1); }

                    return Stack::pop();

                }

};

////////////////////////////////////////////////////////////////


int main()

{

   Stack2 s1; int stackvar; int count = -1;


   for(int i=0; i>=(MAX-1); i++)

   {

       cout << "enter the number into stack: "; cin >> stackvar;

       s1.push(stackvar);

       count++;

   }


   cout << endl;


   while(count != -1)

   {

       s1.pop();

       count--;

   }


    cout << endl;


    system("pause;");

    return 0;

}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Your for loop exits immediately in main():

for(int i=0; i>=(MAX-1); i++)

should be
for(int i=0; i<=(MAX-1); i++)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, WP. I have corrected and it works now.

By the way, don't you think the if statements within the function bodies of Stack2 class become useless because of the for and while loops in the main?

Is there any way that I can use define MAX within the body of Stack class and still be able to do what I want to? As you can see, I initially tried to define MAX within the class body, but then I wanted to use it within the main() so I had to make it global instead.



#include <iostream>

#include <cstdlib>

#include <string>


using namespace std;


const int MAX = 3;

////////////////////////////////////////////////////////////////

class Stack

{

        protected:

            [B][COLOR="#FF0000"]//static const int MAX=10;[/COLOR][/B]

            int st[MAX];

            int top;

        public:

            Stack(): top(-1)

                {/*empty body*/}

        void push(int var)

                { st[++top] = var; }

        int pop()

                { return st[top--]; }

};

////////////////////////////////////////////////////////////////


class Stack2 : public Stack

{

        public:

            void push(int var)

                {

                    [B][COLOR="#FF0000"]if(top >= MAX-1)[/COLOR][/B] // (MAX-1) because array starts from 0

                        { cout << "\nError: stack is full"; exit(1); }


                    Stack::push(var);

                }


            int pop()

                {

                   [B][COLOR="#FF0000"] if(top < 0)[/COLOR][/B]

                        { cout << "\nError: stack is empty\n"; exit(1); }

                    return Stack::pop();

                }

};

////////////////////////////////////////////////////////////////


int main()

{

   Stack2 s1; int stackvar; int count = -1;


   for(int i=0; i<=(MAX-1); i++)

   {

       cout << "enter the number to be pushed onto the stack: "; cin >> stackvar;

       s1.push(stackvar);

       count++;

   }


   cout << endl;


   while(count > -1)

   {

       cout << s1.pop() << endl;

       count--;

   }


    cout << endl;


    system("pause;");

    return 0;

}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
If you make MAX a public member of stack, you could access it as Stack2.MAX.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users