I have to use a link list to pull 10 names from a txt file and then put them in alphabetical order by lastname and then output the list in another file. any help will be greatly appreciated I only have one more day to work on it. Here is what I have so far. I cant figure out how to create a loop to do this.
Code:
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
const int ItemLen = 10;
//Node definition
struct ListNode
{
char name[ItemLen];
char lastname[ItemLen];
ListNode *link;
};
typedef ListNode* ListNodePtr;
ListNodePtr Head;
int main(int argc, char *argv[])
{
char filename[25];
char filelastname[25];
int end;
ListNodePtr p,q,r;
Head= new ListNode;
ifstream inFile;
inFile.open("a:GOLFNAME.txt",ios::in);
//verify that open was successful
if(!inFile.fail()) //if open did not fail
{
cout<<"file Opened"<<endl;
//read name
while(!inFile.eof())
{
inFile>> filename>> filelastname;
cout << "The name is: " << filename <<" "<< filelastname << endl;
strcpy(Head->name,filename);
strcpy(Head->lastname,filelastname);
Head->link= NULL;
}
inFile.close();
//display name
}
else
cout<< "Error opening file." << endl;
// open failed//end if
p=Head;
while(p !=NULL)
{
cout<< p->name << " " << p->lastname << endl;
p= p->link; //Get the next node
}
cin>> end;
return 0;
}
Code tags added by kermi3