This week in my class my professor assigned a project where we have to create a program that reads a txt file and using add, delete, and animation to create a print manager. My professor helped me write code for add and I've got several ideas for animation, but my delete section has a bug in it where the node before the one that is supposed to be deleted is being deleted.
For example, I gave it the instructions of:
A 0 P1 10
A 1 P2 20
A 0 P3 30
A 1 P4 40
D 1 P4
A 1 p5 50
A 2 p6 60
And A 1 P2 was deleted instead of A 1 P4. Any reason why this might be happening? (My code will be down below):
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Node
{
public:
int x, y;
string jobName;
int jobTime;
Node* next;
Node() { jobName = ""; jobTime = -1; next = nullptr; };
Node (string jn, int jt)
{
jobName = jn; jobTime = jt; next = nullptr;
}
};
void Displaypm(Node pm[])
{
for (int i = 0; i < 5; i++)
{
cout << "Location" << i << ": ";
Node* t;
t = &pm[i];
while (t->next != nullptr)
{
cout << t->next->jobTime << " ";
t = t->next;
}
cout << endl;
}
}
void main()
{
string command, jobName;
int location, jobTime;
Node pm[5];
int count[5] = { 0,0,0,0,0 };
bool done = false;
ifstream input("c:\\temp\\input.txt");
system("cls");
while (!input.eof())
{
input >> command;
if (command == "A")
input >> location >> jobName >> jobTime;
else if (command == "B")
input >> location >> jobName;
Node* ptr = new Node(jobName, jobTime);
ptr->y = location * 10;
count[location]++;
ptr->x = count[location] * 10;
while (T->next != nullptr)
{
if (T->next->m_jobName == jobName);
{
if (T->next->next == nullptr)
{
DelPtr = T->next;
T->next = nullptr;
delete(DelPtr);
done = true;
break;
}
else
{
DelPtr = T->next;
T->next = T->next->next;
delete(DelPtr);
done = true;
break;
}
}
T = T->next;
}
}
}
Displaypm(pm);
input.close();