I was given a class Queue that I needed to modify to serve as a class template for a generalized Queue class. I also needed to modify the driver program to test for two additional types of data, which originally only allowed for char data input. I added an int, double, and also a user defined one containing a Date data.
I believe I've modified both files correctly already but I'm having some trouble when I get to inputting a double. Instead of allowing for input, it just automatically skips over as though the while statement it follows has gone out of range before I even get to enter any data.
I also have a minor quirk when inputting ints. I have to input each int on a newline or it will not properly enqueue and dequeue but for the char I can input them all together on one line and it properly separates each character.
#ifndef QUEUE_H
#define QUEUE_H
template<class T>
class Queue
{
struct qNode // definition of a node in queue
{ T data; // data portion of node
struct qNode *nextNode; // pointer to next node (or 0)
};
typedefstruct qNode QNode; // alias for node is QNode
typedef QNode *pQNode; // pointer to a QNode
public:
Queue()
{ headPtr = tailPtr = 0; // no head or tail
curSize = 0; // no nodes
}
bool enqueue( T &c )
{ pQNode newNode = new QNode; // get space for new node
if( newNode != 0 ) // is there any?
{ newNode->data = c; // fill data portion
newNode->nextNode = 0; // terminate
if( isEmpty() ) // if nothing in queue ...
headPtr = tailPtr = newNode; // set new head and tail
else // otherwise ...
{ tailPtr->nextNode = newNode; // chain to new node
tailPtr = newNode; // and reset tail
}
curSize++; // decrement node count
returntrue; // success
}
elsereturnfalse; // failure
}
bool dequeue( T &c )
{ if( isEmpty() ) // anything in queue?
returnfalse;
else
{ pQNode temp = headPtr; // save head
c = headPtr->data; // extract data
headPtr = headPtr->nextNode; // move head
delete temp; // return memory
curSize--; // decrement node count
returntrue;
}
}
int size( void )
{ return curSize; }
private:
pQNode headPtr; // pointer to head of queue
pQNode tailPtr; // pointer to tail of queue
int curSize; // nbr of nodes in queue
bool isEmpty() const // utility function
{ return curSize == 0; }
};
#endif
My main concern right now is with the doubles but I'd also like to know why my ints are working the way they do. Any help with this would be greatly appreciated. Thanks in advance!
I find something unusual in your code - the line #6 in the main file.
As speaking about the "double failure", well... it's not because of your Queue<> class. It's something in istream that triggers the ios_base::failbit. You can switch blocks and put the Queue<double> first or second! There is something at the third, maybe I'll come back later with a clearer explanation after more debugging.
...btw, I just guess that you do this for learning templates. If I'm wrong, what would be the reason for your new structure?
edit:
Your "cin >> tc2" piece of code is responsible for stream reading failure. That terminal character of yours (the hyphen) can not be interpreted as int and neither later as double. After being uninterpreted as int, it remains in the stream, and therefore the next reading of doubles fails even before asking you for new data.