-3

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();

 
  • 1
    Worth a read: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Retired Ninja Feb 20 '22 at 21:31
  • also read https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – pm100 Feb 20 '22 at 21:33
  • 2
    I don't see anything in this code that handles `command == "D"` – Retired Ninja Feb 20 '22 at 21:33
  • 1
    step through with your debugger. Should take about 5 mins to work out – pm100 Feb 20 '22 at 21:34
  • @RetiredNinja well the D case is handled like any other add case, just with empty inputs :-) – pm100 Feb 20 '22 at 21:37
  • The shown code has some major logical problems and confusion. It appears that it expects to parse either "A" or "B" commands, instead of "A" or "D". And no matter what command was entered, "A", "B", "X", or "Rosebud", the shown programs that goes ahead and creates a new node. No matter what is the right input to delete an existing node, does it make sense to you that the process of doing so also results in creating a new node? You need to run your program in your debugger, one line at a time, see what's happening, and figure out if the whole thing makes sense. – Sam Varshavchik Feb 20 '22 at 21:49
  • @RetiredNinja I've been told that before but my professor makes us put it in. He's very set in his ways. – Supreme_Fonzie Feb 20 '22 at 22:04
  • @SamVarshavchik I'll do that and then update you guys with what I found. – Supreme_Fonzie Feb 20 '22 at 22:05
  • @Supreme_Fonzie You professor is doing you a disservice by teaching you bad habits. Whether you use `stream.eof()` or not you should still be checking all input operations for success. By not parsing for `"D"` you're not using empty inputs, you're using whatever was in them from the last iteration. It isn't clear to me what is failing in your code and what part of the code is meant to delete anything. – Retired Ninja Feb 21 '22 at 01:18

1 Answers1

0

I probably didn't get it working in the best way, but I was able to get it working by removing a semicolon after

if (T->next->m_jobName == jobName)

  • So you fixed it by changing code that isn't in the question? – Retired Ninja Feb 21 '22 at 05:11
  • You shouldn't be suggesting fixes that fix issues that weren't in the original question. The extra semicolon is missing in the question. – f4z3k4s Feb 21 '22 at 08:10
  • @RetiredNinja and f4z3k4s That was my bad, I accidentally uploaded the wrong version of the code when I originally uploaded this. I updated the code to the correct version. I feel like an idiot, I'm so sorry guys. – Supreme_Fonzie Feb 23 '22 at 08:10