Search code examples
c++linked-listload-data-infile

Reading names from a .dat file into a linked list


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.


Solution

  • 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 . . .