-
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 ***
-
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 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.
-
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