I am doing a ADT Queue Assigment for my CS class. It uses inheritance. The program compiles but is not working as intended. I won't be posting the base class (a pure virtual class) and the header part of the derived class to save space.
#include "queue.h"
#include <iostream>
#include <string>
usingnamespace std;
struct spaceship
{
int arrivalTime;
string shipName;
int fuelPurchase;
int waitTime;
};
int main()
{
MyQueue<spaceship> A;
MyQueue<spaceship> B;
MyQueue<spaceship> C;
int numShips;
spaceship* customer;
cin >> numShips;
customer = new spaceship[numShips];
cout << A.size() <<endl;
cout << B.size() <<endl;
cout << C.size() <<endl;
for (int i = 0; i < numShips; i++)
{
cin >> customer[i].arrivalTime;
cin >> customer[i].shipName;
cin >> customer[i].fuelPurchase;
customer[i].waitTime = customer[i].arrivalTime + (3 * customer[i].fuelPurchase);
if ((A.size() <= B.size()) && (A.size() <= C.size()))
{
A.enqueue(customer[i]);
cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port A" << endl;
for (int j = 0;)
}
if ((B.size() < A.size()) && (B.size() <= C.size()))
{
B.enqueue(customer[i]);
cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port B" << endl;
cout << B.size() <<endl;
}
if ((C.size() < A.size()) && (C.size() < B.size()))
{
C.enqueue(customer[i]);
cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port C" << endl;
cout << C.size() <<endl;
}
}
delete[] customer;
return(0);
}
#include <iostream>
usingnamespace std;
template<class T>
const MyQueue<T>& MyQueue<T>::operator = (const MyQueue<T>& rhs)
{
if (this != &rhs)
{
if (head != NULL)
clear();
head = NULL;
Node<T>* rhsPtr = rhs.head;
Node<T>* copyPtr = head = new Node<T>;
copyPtr->data = rhsPtr->data;
while (rhsPtr->next != NULL)
{
rhsPtr = rhsPtr->next;
copyPtr->next = new Node<T>;
copyPtr = copyPtr->next;
copyPtr->data = rhsPtr->data;
}
copyPtr->next = NULL;
}
return(*this);
}
template<class T>
MyQueue<T>::MyQueue(const MyQueue<T>& rhs)
{
head = tail = NULL;
*this = rhs;
}
template<class T>
bool MyQueue<T>::isEmpty() const
{
return(head == NULL);
}
template<class T>
const T& MyQueue<T>::front() constthrow(Oops)
{
if (head != NULL)
return(head->data);
elsethrow Oops("Error");
}
template<class T>
const T& MyQueue<T>::back() constthrow(Oops)
{
if (tail != NULL)
return(tail->data);
elsethrow Oops("Error");
}
template<class T>
void MyQueue<T>::enqueue(const T& x)
{
Node<T>* newNode = new Node<T>;
newNode->data = x;
newNode->next = NULL;
if (isEmpty())
{
tail = newNode;
head = tail;
}
else
{
tail->next = newNode;
tail = tail->next;
}
}
template<class T>
void MyQueue<T>::dequeue()
{
Node<T>* temp = head;
if (isEmpty())
return;
if (head == tail)
head = tail = NULL;
else
{
head = head->next;
delete temp;
}
}
template<class T>
void MyQueue<T>::clear()
{
Node<T>* del;
del = head;
while (head != NULL)
{
head = head->next;
delete del;
del = head;
}
}
template<class T>
int MyQueue<T>::size() const
{
Node<T>* temp;
temp = head;
int count = 0;
while (temp != NULL)
{
++count;
temp = temp->next;
}
return(count);
}
After I enter the first ships info (arrival time, name, fuel units purchased), it is being queued into all 3 MyQueue. I only need it to be queued to the shortest line (tiebreaker goes by alphabet). I have no idea why is this. Any help would be appreciated.
Add "else" on lines 45 and 51. Also, get rid of line 153 and 156. You might also want to check that rhs is not empty in your assignment operator function as well.