stack debug

This is a discussion on stack debug within the C++ Programming forums, part of the General Programming Boards category; Could someone help me debug this. I am stumped. Code: // Stack of customers prblm 3 pg 417 c++ primer ...

  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 09:22 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    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.
    I used to be an adventurer like you... then I took an arrow to the knee.

  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, 09:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05: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

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