JackDurden 0 Junior Poster in Training

How do I get the data from file into the Insert function? Or more general how do you get data from a file and put it into a linked list?

#include <iostream>
#include <fstream>

using namespace std;

class List
{
public:
	void Insert();
	void Print();
};

struct node
{
	int age;
	node *nxt;
}; 
node *start_ptr = 0;
node *current;

void List::Insert()
  {  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node; 
	 temp->age;
    
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { start_ptr = temp;
	 current = start_ptr;
       }
     else
       { temp2 = start_ptr;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
  }
void List::Print()
  {  node *temp;
     temp = start_ptr;
     
     if (temp == NULL)
       cout << "The list is empty" << endl;
     else
       { while (temp != NULL)
	   {  // Display details for what temp points to
              cout << "Age : " << temp->age << " ";
	   
	      if (temp == current)
		cout << " <-- Current node";
              cout << endl;
	      temp = temp->nxt;

	   }
	 cout << "End of list!" << endl;
       }
  }
void main()
  {  
	  start_ptr = NULL;
	  List display;
	  fstream inFile;
	  node number;
	  inFile.open("int.txt");

	  inFile>>number.age;

	display.Print();
	display.Insert();
	
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.