Help with a stack

This is a discussion on Help with a stack within the C++ Programming forums, part of the General Programming Boards category; I keep getting errors in my code and I can't seem to fix it. Any Idea what it could be? ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Question Help with a stack

    I keep getting errors in my code and I can't seem to fix it. Any Idea what it could be?

    Code:
    #include<iostream>
    using namespace std;
    
    class stack{
    private:
      int stackArray[3];
      int top;
      int size;
    public:
      void push(int x);
      int pop();
      int showSize();
      void showElements();
      stack();
      ~stack();
    };
    
    stack::stack(){
      
      top = -1;
      size = 0;
      
    }
    
    stack::~stack(){
    }
    
    void stack::push(int x){
      if(size < 3}{
        top++;
        stackArray[size] = x;
        size++;
      }
    }
    
    int stack::pop(){
      int x = stackArray[top];
      top--;
      size--;
      return x;
    }
    
    int stack::showSize(){
      return size;
    }
    
    void stack::showElements(){
      
      for(int i = top; i > -1; i--){
        cout<<stackArray[i]<<endl;
      }
    }
    
    
    int main(){
      
      int val;
      stack s;
      s.push(5);
      s.push(4);
      s.push(3);
      cout<<"Size: "<<s.showSize()<<endl;
      cout<<"Elements Pushed: ";
      s.showElements();
      
      val = s.pop();
      s.pop();
      s.pop();
      cout<<"Last element Popped"<<val<<endl;
      s.showElements();
      
      
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    I get these errors..

    In member function `void stack:ush(int)':
    onetwo.cpp:29: error: parse error before `}' token
    onetwo.cpp: At global scope:
    onetwo.cpp:31: error: `size' was not declared in this scope
    onetwo.cpp:31: error: ISO C++ forbids declaration of `stackArray' with no type
    onetwo.cpp:31: error: `x' was not declared in this scope
    onetwo.cpp:31: error: assignment (not initialization) in declaration
    onetwo.cpp:32: error: syntax error before `++' token

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    Code:
    if(size < 3}{
    should be:
    Code:
    if(size < 3){
    Note the } versus )
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    wow thanks a bunch..i couldn't find that tiny little error!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21