Hello, I have to make this program that has two queue's. It takes input from a file, and each line in the file has a number and character. If the number is a one, the character is stored in the highQueue. If it is a 2-5 it is stored in the lowQueue. If the character is a *, it outputs the current characters stored, going highQueue, lowQueue, highQueue, etc. I am having trouble getting it to output correctly.

Here is the class with the main mathod:

#include "queue.cpp"
#include <iostream>
#include <fstream>
#include <iomanip>

/*******************************************
* class Priority                           *
* Takes input from file "queue1.dat"       *
* If that input is '*' calls service()     *
* If that input is '=' exits program       *
* Otherwise, Sends that input to queue.cpp *
* Author: Kimberlie Davis                  *
* Version: 2/5/09                          *
********************************************/

class Priority:public Queue
{
     protected:
              int priority;
              char character; 
     public:
            void service(Queue&, Queue&);
                 
};

/*
* method service
* While queue is not empty,
* Output contents of queue
*/
void
Priority::service(Queue &b, Queue &c)
{
    char lowOutput;
    char highOutput;
    cout << "Service: ";
    while(b.count != 0)
    {
       lowOutput = b.takeAway();
       highOutput = c.takeAway();
       cout << highOutput << " " << lowOutput;
    }             
    
    cout << endl;   
}

/*
* main method
* Takes input from file
* Tests if file exists
* Tests if file is open
* While not end of file, tests files contents
* Calls correct method based on file contents
*/
int main()
{
    Priority priority;
    Queue highQueue;
    Queue lowQueue;
    ifstream partin;
    char charInput;
    int numInput;
    bool highQ;
    partin.open("queue1.dat");
    
    lowQueue.count = 0;
    
    if(!partin)
       cout << "Error: Unable to find file" << endl;
    if(partin.fail())  
        cout << "Error: Unable to open file!" << endl; 

    partin >> charInput >> numInput;
    while(partin)
    {
       if(charInput == '=')
           system("exit");
       else if(charInput == '*')
       {
            
            priority.service(lowQueue, highQueue);
       }
       else
       {
           if(numInput == 1)
           {
               highQueue.addToQueue(charInput); 
               highQ = true;
           }
           else
           {
                lowQueue.addToQueue(charInput);
                highQ = false;
           }
       }
           
        partin >> charInput >> numInput; 
    }
    
    partin.close(); 
    system("pause");
}

And here is the class that deals with the queue:

/****************************************
* A program that stores data to a queue *
* Tests if queue is full or empty       *
* Cant take data out of queue           *
* Author: Kimberlie Davis               *
* Version: 2/5/09                       *
*****************************************/
#include <iostream>

using namespace std;

class Queue
{
 private:
        int fill, remove;
        char element[6];
 public:
        Queue(void);
        void initialize(void);
        char takeAway();
        bool full(void);
        bool empty(void);
        void addToQueue(char);
        char highPriority[5];
        int count;
        char lowPriority[5]; 
};

/*
* Constructor
* Initializes fill, remove and count
*/
Queue::Queue(void)
{
      fill = -1;
      remove = 0;
      count = 0;      
}

/*
* method initialize
* Reinitializes the variables
*/
void
Queue::initialize()
{ 
      fill = -1;
      remove = 0;
      count = 0;                     
}
/*
* Method takeAway
* Removes items from the queue
*/
char
Queue::takeAway()
{
      char value;
      bool isEmpty;
      isEmpty = empty();  

      while(count != 0) 
      {
           value = element[remove];
           remove = (remove+1)%5;
           count--;
           fill--;
           return value;
      }       
         
}

/*
* method full
* Returns true if queue is full
*/
bool
Queue::full()
{
      if(count == 5)
      {
         cout << "Queue Overflow" << endl;
         return true;
      }
      else
         return false; 
}

/*
* method empty
* returns true if queue is empty
*/
bool
Queue::empty()
{
      if(count == 0)
      {
         cout << "Queue Empty" << endl;
         return true;
      }
      else
         return false;           
}

/*
* method addToQueue
* Tests if value is of high or low priority
* Adds the value to the correct queue
*/
void 
Queue::addToQueue(char character)
{
      if(count == 5)
         cout << "Queue overflow" << endl;
      else
      {
         fill = (fill+1)%5;
         element[fill] = character;
         count++;
      }                 
}

Recommended Answers

All 3 Replies

Try to be more specific in your description of your problem. If you say you are supposed to alternate popping the elements of each queue until both queues are empt displaying them to the screen as you go if the char in the file is an asterisk, then I'd expand the interface of the queue class to include methods like top() to display the "oldest" element currently in the queue, pop() to remove a single element from the queue, and push() to add a single element to a queue. If desired, I would then reimplement the takeAway() and addToQueue() elements in terms of pop() and push() respectively. Further, I would increment and decrease count with each call to push and pop so count never reaches five and the modula never needs to be used.

Well, I think my main problem is getting it to work with two different queue objects...it works with just one.

addToQueue() adds a single char to the queue with each function call but I don't think it is optimal for queue inplementation, meaning it will be difficult to keep track of which element has been in the queue the longest so it can be the next one removed.

takeAway() uses a loop to empty the entire queue with a single call. If that's what you want, fine. But the problem you are describing seems to require display the top of one queue, followed by popping it of the queue followed by the display of the top of the second queue before popping it off and then alternating back and forth. The alternation requires you to rewrite the process since you can't just do it from queue one until empty and then queue 2 until empty. If I'm interpreting the requirements wrong and you can empty all of queue one before emptying all of queue 2, then takeAway() is okay. Usually, though, queues are implemented such that popping is done one element per call. It's okay to have a empty the queue function like takeAway(), but, in my opinion, you should also have the ability to just pop the top.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.