Thread: stack debug

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    stack debug

    Could someone help me debug this. I am stumped.
    Code:
    // Stack of customers prblm 3 pg 417 c++ primer plus
    #ifndef _STACKH_H_
    #define _STACKH_H_
    
    struct customer
    {
    	char fullname[35];
    	double payment;
    };
    
    typedef customer Item; //error here
    
    class Stack // error here
    {
    private:
    	enum {MAX=10};
    	Item items[MAX];
    	int top;
    public:
    	Stack();
    	bool isempty() const;
    	bool isfull() const;
    	// push returns false if stack is full, else true
    	bool push(const Item & item); // add item to stack
    	// pop returns false if stack is empty, true otherwise
    	bool pop(Item & item);
    };
    #endif
    C:\Borland\BCC55\src>bcc32 stackh.h
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    stackh.h:
    Error E2257 stackh.h 11: , expected
    Error E2141 stackh.h 13: Declaration syntax error
    *** 2 errors in Compile ***
    Last edited by rippascal; 04-04-2002 at 10:22 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Don't know about the errors you get because I don't know what the line numbers coorespond to in the code sample you provided. However, can't you just use the built in stack class?

    Code:
    #include <stack>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    struct customer
    {
    	char fullname[35];
    	double payment;
    };
    
    int main()
    {
        stack<customer> MyStack;
        customer Item;
    
        strncpy( Item.fullname, "Billy", sizeof(Item.fullname) );
        Item.payment = 100.0;
        MyStack.push( Item );
    
        strncpy( Item.fullname, "Bob", sizeof(Item.fullname) );
        Item.payment = 200.0;
        MyStack.push( Item );
    
        strncpy( Item.fullname, "Thornton", sizeof(Item.fullname) );
        Item.payment = 300.0;
        MyStack.push( Item );
    
        while( !MyStack.empty() )
        {
            Item = MyStack.top();
            cout << "Name is : " << Item.fullname << ", payment is: " <<
    			Item.payment << '.' << endl;
            MyStack.pop();
        }
    
        return 0;
    }
    On my machine this prints out the following:

    Name is : Thornton, payment is: 300.
    Name is : Bob, payment is: 200.
    Name is : Billy, payment is: 100.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    I haven't learned about the built in stack class and prefer to step along with the book. By doing so I learn more about implementing a stack class.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    I have the program working. It doesnt work when I use the extension .h but if i use the extension .cpp and change #ifndef and #define their is no problem. compare this code with that of my previous post (I believe .h is a C header file extension and c++ uses somthing different)
    Code:
     
    // Stack of customers prblm 3 pg 417 c++ primer plus
    #ifndef _STACKH_CPP_
    #define _STACKH_CPP_
    
    struct customer
    {
    	char fullname[35];
    	double payment;
    };
    
    typedef customer Item;
    
    class Stack
    {
    private:
    	enum {MAX=10};
    	Item items[MAX];
    	int top;
    public:
    	Stack();
    	bool isempty() const;
    	bool isfull() const;
    	// push returns false if stack is full, else true
    	bool push(const Item & item); // add item to stack
    	// pop returns false if stack is empty, true otherwise
    	bool pop(Item & item);
    };
    #endif

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. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM