Queue Class Template

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.

Here is the code for the driver test program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstdlib>
using namespace std;

#include "queue.h"
#include "date.cpp"

int main( void )
{
  //CHAR INPUT -- works!
  Queue<char> charQueue;
   char tc1;
   
   cout << "Enter chars to enqueue, '-' to end queue for chars:\n";  
   while( (cin >> tc1) && tc1 != '-')
      charQueue.enqueue( tc1 );
      
   cout << "\n" << charQueue.size() << " chars are queued";
   
   cout << "\n\nDequeued chars:\n";
   while( charQueue.dequeue( tc1 ) )
      cout << tc1 << ' ';
      
   cout << "\n\n" << charQueue.size() << " chars are queued";
   cout << endl << endl;
   
  //INT INPUT -- works if separated by /n!
  Queue<int> intQueue;
   int tc2;

   cout << "Enter ints to enqueue, '-' to end queue for ints:\n";  
   while( (cin >> tc2) && tc2 != '-')
      intQueue.enqueue( tc2 );

   cout << "\n" << intQueue.size() << " ints are queued";

   cout << "\n\nDequeued ints:\n";
   while( intQueue.dequeue( tc2 ) )
      cout << tc2 << ' ';

   cout << "\n\n" << intQueue.size() << " ints are queued";
   cout << endl << endl;
   
 //DOUBLE INPUT -- skips through?  
 Queue<double> dblQueue;
   double tc3;
   cout << "Enter doubles to enqueue, '-' to end queue for dbls:\n";  
   while( (cin >> tc3) && tc3 != '-')
     dblQueue.enqueue( tc3 );
     
   cout << "\n" << dblQueue.size() << " doubles are queued";

   cout << "\n\nDequeued dbls:\n";
   while( dblQueue.dequeue( tc3 ) )
      cout << tc3 << ' ';

   cout << "\n\n" << dblQueue.size() << " doubles are queued";
   cout << endl << endl;
  
  //DATE INPUT  -- works!
  Queue<Date> dateQ;
   Date mom(12, 15, 1960);
   Date dad(8, 5, 1959);
   Date bro(1, 16, 1991);

   dateQ.enqueue(mom);
   dateQ.enqueue(dad);
   dateQ.enqueue(bro);

   cout << "\n" << dateQ.size() << " Dates are queued";   
   cout << "\n\nDequeued dates:\n";

   dateQ.dequeue(mom);
   cout << mom << endl; 
   dateQ.dequeue(dad);
   cout << dad << endl;   
   dateQ.dequeue(bro);
   cout << bro << endl;   

   cout << "\n\n" << dateQ.size() << " Dates are queued";
   cout << endl << endl;  
   
   system("PAUSE");
   return 0;
}


Here is my class template:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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)
      };
   typedef struct 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
               return true;                        // success
            }
            else
               return false;                       // failure
         }
      bool dequeue( T &c )
         {  if( isEmpty() )                        // anything in queue?
               return false;
            else
            {  pQNode temp = headPtr;              // save head
               c = headPtr->data;                  // extract data
               headPtr = headPtr->nextNode;        // move head
               delete temp;                        // return memory
               curSize--;                          // decrement node count
               return true;
            }
         }
      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!

P.S. My current output: http://cl.ly/1d370v0b420m322H2Q1c
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.