Reading file into a Linked List.

I'm having some trouble with reading a file into a linked list. It reads in properly, but I am trying to sort it by first name...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int main()      
{  // Creat a linked list of Presidents in sorted order
   //      (internally)
    
    
    class nodeType	//We will use this nodeType for this program, other one can be ignored
    {  
    public:
		char president[256];	//char Array with 256 characters 
       nodeType *link; //pointer variable
    }; 
    
    nodeType *head;	// Head of our list
    head = NULL; //Head is NULL for now
   
    
    string FileName = "C:\\Users\\Jordan\\Desktop\\Documents\\College\\Senior Year\\Spring 2013\\Data Structures\\Programs\\Presidents.txt";
    ifstream fin; 
    fin.open(FileName.data());          
   
    for (int i = 0 ; i < 100 ; i++)
    {
	  nodeType *temp = new nodeType;	//get memory allocation for  anew node 
          
      fin.getline(temp->president, 256); //read in the line from file
        
        
        
      //  PUT INPUT COMMAND HERE          Delete this:  temp->president = " ";  

      if (head == NULL)   //If empty list  
         { 
		 	temp->link = head ;  // link our node to the head
            head = temp ; //make our node the head now
         }  
      else 
         { 
		 	nodeType *hunter2 = head;	//declare another node
            for(; hunter2->link != NULL; hunter2 = hunter2->link )   //traverse through whole list
              { 
                if ((hunter2->link)->president == temp->president ) break ;   //break out when the next spot equal    
              }        
              temp->link = hunter2->link; 
              hunter2->link  = temp; 
         }       
    }    
   
    // Traverse the linked list 
    nodeType *hunter = head;
    string tester;
    while (hunter != NULL)
    {
    	tester = hunter->president;
    	if (!tester.empty())
    	{
			cout << hunter->president << "\n";
			delete hunter;
    	}
		hunter = head;
    }
    
    cout << "\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


This list is created internally in main..

Look at line 41
if ((hunter2->link)->president == temp->president ) break ;

This is the line that should compare them and help put them in the correct spot. For some reason, no matter if I do == > or < it doesn't work. But for now, with == the list is imported exactly how it is presented in the file...

The file is a list of Presidents names and years of service looking like this...

George Washington (1789-1797)
John Adams (1797-1801)
Thomas Jefferson (1801-1809) ...etc...
Topic archived. No new replies allowed.