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++;
}
}