title: Reading from txt file into a linked list
body: <p>I am attempting to read data from a *.txt file into a linked list. The file is set up with 16 different types of data arranged into rows, with tabs between each data type. I am having several problems with this. The compiler gives no errors, but when I run the program and enter the file name to be read from, nothing happens. I tried replacing the <code>while(!din.eof())</code> line (line 27 below) with a for loop <code>for(int i=0; i&lt;NUM_LINES; i++)</code>, and this seemed to help, but I was given the first three elements (all string types) followed by a row of zeroes, with this line repeating until the end of the loop. I've included the method in which I believe the problem is located below. I was wondering if someone could take a look at my code and let me know where the problem is. Thanks.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  class Node
  {
   public:
     // Constructors
     Node();
     Node(const string name, const string position, const string team, const int g, const int att, const float attg, const int cmp, const float pct, const int yds, const float ydsg, const int lng, const int td, const int inte, const int sck, const int sckyl, const float rating);
     Node(const Node &amp; node);
     ~Node();
  
     // Methods
     bool readData();
     void setNext(Node* next);
     void print() const;
  
   private:
     string Name;
     string Position;
     string Team;
     int G;
     int Att;
     float AttG;
     int Cmp;
     float Pct;
     int Yds;
     float YdsG;
     int Lng;
     int TD;
     int Int;
     int Sck;
     int SckYL;
     float Rating;
     Node *Next;
  };
  
  //----------------------------------------------
  // Constructor method
  //----------------------------------------------
  Node::Node()
  {
     Name = "";
     Position = "";
     Team = "";
     G = 0;
     Att = 0;
     AttG = 0.0;
     Cmp = 0;
     Pct = 0.0;
     Yds = 0;
     YdsG = 0.0;
     Lng = 0;
     TD = 0;
     Int = 0;
     Sck = 0;
     SckYL = 0;
     Rating = 0.0;
     Next = NULL;
  }
  
  //----------------------------------------------
  // Constructor method with parameters
  //----------------------------------------------
  Node::Node(const string name, const string position, const string team, const int g, const int att, const float attg, const int cmp, const float pct, const int yds, const float ydsg, const int lng, const int td, const int inte, const int sck, const int sckyl, const float rating)
  {
     Name = name;
     Position = position;
     Team = team;
     G = g;
     Att = att;
     AttG = attg;
     Cmp = cmp;
     Pct = pct;
     Yds = yds;
     YdsG = ydsg;
     Lng = lng;
     TD = td;
     Int = inte;
     Sck = sck;
     SckYL = sckyl;
     Rating = rating;
     Next = NULL;
  }
  
  //----------------------------------------------
  // Copy constructor method
  //----------------------------------------------
  Node::Node(const Node &amp; node)
  {
     Name = node.Name;
     Position = node.Position;
     Team = node.Team;
     G = node.G;
     Att = node.Att;
     AttG = node.AttG;
     Cmp = node.Cmp;
     Pct = node.Pct;
     Yds = node.Yds;
     YdsG = node.YdsG;
     Lng = node.Lng;
     TD = node.TD;
     Int = node.Int;
     Sck = node.Sck;
     SckYL = node.SckYL;
     Rating = node.Rating;
     Next = NULL;
  }
  
  //----------------------------------------------
  // Destructor method
  //----------------------------------------------
  Node::~Node()
  {
  }
  
  //----------------------------------------------
  // Method:   readData
  // Purpose:  Read data from file into linked list, and print contents of the list.
  //----------------------------------------------
  bool Node::readData()
  {
     // Declare local variables
     string name, position, team;
        name = position = team = "";
     int g, att, cmp, yds, lng, td, inte, sck, sckyl;
        g = att = cmp = yds = lng = td = inte = sck = sckyl = 0;
     float attg, pct, ydsg, rating;
        attg = pct = ydsg = rating = 0.0;
  
     // Get file name
     string filename = "";
     cout &lt;&lt; "Enter file name: ";
     cin &gt;&gt; filename;
  
     // Open input file
     ifstream din;
     din.open(filename.c_str());
     if (din.fail())
     {
        cerr &lt;&lt; "Could not open file: " &lt;&lt; filename &lt;&lt; endl;
        return false;
     }
  
     // Read data
     Node *head = NULL;
     while (!din.eof())
     {
        din &gt;&gt; name &gt;&gt; position &gt;&gt; team &gt;&gt; g &gt;&gt; att &gt;&gt; attg &gt;&gt; cmp &gt;&gt; pct &gt;&gt; yds &gt;&gt; ydsg &gt;&gt; lng &gt;&gt; td &gt;&gt; inte &gt;&gt; sck &gt;&gt; sckyl &gt;&gt; rating;
  
        Node *temp = new Node(name, position, team, g, att, attg, cmp, pct, yds, ydsg, lng, td, inte, sck, sckyl, rating);
        temp-&gt;setNext(head);
        head = temp;
     }
  
     din.close();
     head-&gt;print();
     return true;
  }
  
  //----------------------------------------------
  // setNext method
  //----------------------------------------------
  void Node::setNext(Node* next)
  {
     Next = next;
  }
  
  //----------------------------------------------
  // Print method
  //----------------------------------------------
  void Node::print() const
  {
     cout &lt;&lt; Name &lt;&lt; "    " &lt;&lt; Position &lt;&lt; "    " &lt;&lt; Team &lt;&lt; "    " &lt;&lt; G &lt;&lt; "    " &lt;&lt; Att &lt;&lt; "    " &lt;&lt; AttG &lt;&lt; "    "
          &lt;&lt; Cmp &lt;&lt; "    " &lt;&lt; Pct &lt;&lt; "    " &lt;&lt; Yds &lt;&lt; "    " &lt;&lt; YdsG &lt;&lt; "    " &lt;&lt; Lng &lt;&lt; "    " &lt;&lt; TD &lt;&lt; "    " &lt;&lt; Int &lt;&lt; "    "
          &lt;&lt; Sck &lt;&lt; "    " &lt;&lt; SckYL &lt;&lt; "    " &lt;&lt; Rating;
     if (Next != NULL)
        Next-&gt;print();
  }
  
  //----------------------------------------------
  // Main program
  //----------------------------------------------
  
  int main()
  {
     Node list;
     if (list.readData())
        cout &lt;&lt; "Success" &lt;&lt; endl;
     else
        cout &lt;&lt; "Fail" &lt;&lt; endl;
  
     return 0;
  }
  </code></pre>