0

I'm still having trouble figuring out how to keep track of my linked lists but have come up with some progress.

My code is reading the data from the text file perfectly through the nodes but I cannot seem to figure out how to transfer it properly to the show function to show all the nodes.

READING FROM FILE (not working correctly)

void insertAsFirstElement(ifstream& budgetFile, budgetItem *&head, budgetItem *&last, int number, int& counter)
{
int ctype;
string cname;
double camount;
char clearOrNot;


while (!budgetFile.eof())
{
budgetItem *temp = new budgetItem;


    budgetFile >> ctype >> cname >> camount >> clearOrNot;

    temp->theType = ctype;
    cout << temp->theType << endl;
    //cout << ctype << endl;
    temp->name = cname;
    cout << temp->name << endl;
    temp->amount = camount;
    cout << temp->amount << endl;
    if (clearOrNot == 'Y')
    {
        temp->cleared = true;
    }
    else
    {
        temp->cleared = false;
    }

    last = temp;

temp->next = NULL;

if (counter == 0)
{
    //head = temp;
    head = temp;
}
    counter++;
}
}

SHOWING THE DATA IN THE NODES (only one node's data is showing...) *I need all the nodes and all their data showing.(must have something to do with reading)

void showList(budgetItem *current)
{
if (isEmpty(current)) {
    cout << "The list is empty." << endl;
}
else
{
    cout << "The list contains: " << endl;

    while (current != NULL)
    {
        cout << current->theType << " ";
        cout << current->name << " ";
        cout << current->amount << " ";
        cout << current->cleared << endl;

        current = current->next;
    }
}
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Bubz21
  • 15
  • 1
  • 4
  • Related: Read this: ["Why is `iostream:eof` inside a loop condition considered wrong?"](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Mar 01 '14 at 03:21
  • I read it now. Thanks for the tip – Bubz21 Mar 01 '14 at 03:33
  • Any clue as far as the problem at hand, though? – Bubz21 Mar 01 '14 at 03:52
  • I stopped reading at `while (!budgetFile.eof())` – Lightness Races in Orbit Mar 01 '14 at 03:56
  • `I cannot seem to figure out how to transfer it properly to the show function to show all the nodes` This is not a technical description of a problem. List inputs, outputs, and how those outputs differ from your expectation. In detail. – Lightness Races in Orbit Mar 01 '14 at 03:56
  • How do you know your linked list is built properly? It would seem given the complexity of the two tasks, properly building during reading from a file vs. walking a node pointer and sending to output, the latter is considerably easier to implement and would clearly expose a problem with the former. Apart from the unusual `isEmpty(..)` construct, your print loop looks right, btw. In other words, your assertion your loader is working "perfectly" isn't valid. – WhozCraig Mar 01 '14 at 03:56
  • You're right about all that, actually. I have been finding reading from a file much more difficult. – Bubz21 Mar 01 '14 at 04:31

2 Answers2

0

You should use std::list from C++ STL library for your program. You can use push_back() function to insert into the std::list object.

Once you have stored the nodes in the std::list, you can use iterator based loop to read and display your element. You would have to overload >> and << operator for your structure budgetItem in this case.

Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48
0

Though I disagree with most of how this code is laid out, but the issue of wiring your linked list is the following:

last = temp;
temp->next = NULL;

if (counter == 0)
{
    //head = temp;
    head = temp;
}

You're abandoning whatever last pointed to before, therefore there is no link established. The ensuing memory leak is a bonus feature, I suppose.

Do this:

temp->next = NULL;

if (last != NULL)
    last->next = temp;
else
    head = temp;
last = temp;

Your code could be cleaned up considerably starting with properly determining when to stop reading. Modularizing budgetItem so it is wise enough to read itself from a std::istream would be a good step as well.

Finally, I assume this is for academia, otherwise i'd tell you to not reinvent the wheel and use std::vector<budgetItem> in the first place.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • For some reason the vidoes and information I was getting on linked lists had me putting my lists in the manner I did. I can understand the basics of them except to the point of reading/displaying data from an actual file. – Bubz21 Mar 01 '14 at 04:29
  • Thank you so much for all your help btw. I'm going to take what you said into consideration for further programs. – Bubz21 Mar 01 '14 at 04:39