Reading names from a .dat file into a linked list

178 views Asked by At

I've been having some trouble storing and printing data from this file into my linked list.

File Info:

Brown Aniya
Canaday Jordan
Germano Thomas
Grant Kiara
Gunathilaka Piyumi
Johnson Demetria
McGill Jayla
Mitchell Ty-Jaa'
Robertson Victoria
Taylor Chloe
Thomas Amon
Watkins Malkym
Wyatt Arkasia

Here's the current output:

Brown Aniya

--------------------------------
Process exited after 0.1335 seconds with return value 0
Press any key to continue . . . 

The main:

#include <iostream>
#include <fstream>
#include "P4-linkedList_Cox.h"

using namespace std; 

int main()
{
    tests oh;

    oh.getNames();
    oh.print();




   return 0;
}

the implementation:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "P4-linkedList_Cox.h"

using namespace std;

void tests::getNames()
{
    ifstream inData;
    inData.open("infile.dat");

    nameType *newNode, *nodePtr;

   newNode = new nameType;
   newNode->link= NULL; 
   string um;
        getline(inData, um);
        newNode->info = um;


   if (head != NULL)
   {
      nodePtr = head;

      while (nodePtr->link)
      {
         nodePtr = nodePtr->link;               
      }
     nodePtr->link = newNode;       
   }
   else 
    head = newNode;
}

void tests::print()
{
   nameType *nodePtr;
   nodePtr = head;
   while (nodePtr != NULL)
   {
      cout << nodePtr->info << endl;
      nodePtr = nodePtr->link;
   }


}

tests::tests()
{
    head = NULL;

}

and the .h:


using namespace std;

class tests
{
    public:
        void getNames();
        void print();
        tests();


    private:
    struct nameType
    {
        string info;
        nameType *link;

    };

    nameType *head;
};      

Linked lists are still pretty confusing for me, so any help is welcome! I also need more words in this post, I think I explained my problem pretty clearly though. I just need help storing all the names from the .dat file into my linked list and then output that list.

2

There are 2 answers

0
Yasmeen On BEST ANSWER

Thank you so much for the help, however, I manage to get the solution by getting the user to input the number of students then passing that to the function to create a loop. Thanks again for the advice though!

Fixed implementation:

void tests::getNames(int students)
{
    ifstream inData;
    inData.open("infile.dat");  

    for(int i = 0; i < students; i++)
    {   
        nameType *newName, *namePtr;
        string um;
        newName= new nameType;
        newName->link= NULL;        
        getline(inData, um);
        newName->info = um;

   if (!head)
      head = newName;
   else  
   {
      namePtr = head;

      while (namePtr->link)
         namePtr = namePtr->link;

      namePtr->link = newName;
   }    

    }

}

Output:

Enter the number of students: 13
Brown Aniya
Canaday Jordan
Germano Thomas
Grant Kiara
Gunathilaka Piyumi
Johnson Demetria
McGill Jayla
Mitchell Ty-Jaa'
Robertson Victoria
Taylor Chloe
Thomas Amon
Watkins Malkym
Wyatt Arkasia

--------------------------------
Process exited after 3.865 seconds with return value 0
Press any key to continue . . .
0
Shaavin On

It looks like you create a single node in your getNames() method. For each insertion, you will need to create a new node, fill in that node, and then add it to the linked list, which will need to be in a loop of its own (your current loop just looks for the end of the linked list). Plus, because you initialize the head of the linked list to NULL, you never enter your loop to begin with when you perform the test if(head != NULL). This can be accomplished with something like:

while(inputFile >> firstName >> lastName) { //reiterate while there is more information to read in
    Person tempPerson(firstName, lastName); //create a new person -- I'm assuming this class exists here with an appropriate constructor
    Node *tempNode = new Node(tempPerson, nullptr); //create a new node on the heap with this person and no next node

    if(head != nullptr) {
        Node* prevNodePtr = nullptr, currNodePtr = head; //start with the head of the list
        while(currNodePtr != nullptr) { //find the end of the list -- currNodePtr will be nullptr when at the end
            prevNodePtr = currNodePtr;
            currNodePtr = currNodePtr->getNextPtr(); //shift both pointers over once
        }
        prevNodePtr->setNext(tempNode); //have the current end of the list point to our new node, making our new node the new end of the list
    }
    else { //head is nullptr, therefore there is not currently a list
        head = tempNode; //change the head to point to this new node
    }
}

Sidebar: you wanna be sure to close the input file after you're done using it!