title: C++ - Reading a Table from a Text File
body: <p>I have been trying to work on an assignment for class where I am supposed to read a table containing students' names and grades from a file called table.txt and store the values in C++ variables, and from there I need to manipulate the grade data, then present it all to the user via tables.
  <strong>When I run my code, the necessary tables are created, but the data does not appear. The row count stays at 0, and the Class Average comes up to -nan(ind).</strong> I'm afraid this might be an issue with the opening of the text file itself, but I'm not sure. This is where I need your help, and any help would be greatly appreciated!
  I apologize if my request seems simple or if my code is horrendous, as I'm new to the programming field!</p>
  <p>table.txt</p>
  <pre><code>Anderson    91.5    95
  Blake   75.5    90
  Cheg    0   0
  Dang    95  85
  Engberg 80  100
  Farris  55  90
  Garcia  93.6    90.5
  Hadad   65  60
  Ionescu 100 95.5
  Johnson 75  90
  Kaloo   75  85
  Lagos   55.5    80
  Mikhailov   75  83.5
  Nguyen  95  100
  O'Neil  85  70
  </code></pre>
  <p>My current code</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  int main()
  {
      ifstream fin(&quot;table.txt&quot;);
      int numStudents = 0;
      double avgNumGrade = 0.0;
      cout &lt;&lt; setw(30) &lt;&lt; &quot;STUDENT STATISTICS&quot; &lt;&lt; endl;
      cout &lt;&lt; setfill(' ') &lt;&lt; setw(15) &lt;&lt; &quot;Student Name&quot; &lt;&lt; setw(15) &lt;&lt; &quot;Total Points&quot; &lt;&lt; setw(15) &lt;&lt;  &quot;Numeric Grade&quot; &lt;&lt; setw(15) &lt;&lt; &quot;Letter Grade&quot; &lt;&lt; endl;
      string line;
      while (getline(fin, line)) {
          string name;
          double testGrade;
          int assnGrade;
          double totalPoints;
          double numGrade;
          char letterGrade;
  
          fin &gt;&gt; name;
          cout &lt;&lt; setw(10) &lt;&lt; left &lt;&lt; name &lt;&lt; setfill(' ');
          fin &gt;&gt; testGrade;
          fin &gt;&gt; assnGrade;
          totalPoints = testGrade + assnGrade;
          numGrade = totalPoints / 2;
          if (numGrade &lt;= 100 &amp;&amp; numGrade &gt;= 89.5) {
              letterGrade = 'A';
          }
          else if (numGrade &lt;= 89.49 &amp;&amp; numGrade &gt;= 79.5) {
              letterGrade = 'B';
          }
          else if (numGrade &lt;= 79.49 &amp;&amp; numGrade &gt;= 69.5) {
              letterGrade = 'C';
          }
          else if (numGrade &lt;= 69.49 &amp;&amp; numGrade &gt;= 59.5) {
              letterGrade = 'D';
          }
          else if (numGrade &lt;= 59.49 &amp;&amp; numGrade &gt;= 0) {
              letterGrade = 'F';
          }
          cout &lt;&lt; setw(5) &lt;&lt; right &lt;&lt; setprecision(3) &lt;&lt; totalPoints &lt;&lt; setfill(' ') &lt;&lt; numGrade &lt;&lt; setfill(' ');
          cout &lt;&lt; setw(5) &lt;&lt; letterGrade &lt;&lt; endl;
          numStudents++;
          avgNumGrade = avgNumGrade + numGrade;
      }
      avgNumGrade = avgNumGrade / numStudents;
  
      cout &lt;&lt; setw(30) &lt;&lt; &quot;CLASS STATISTICS&quot; &lt;&lt; endl;
      cout &lt;&lt; setw(15) &lt;&lt; left &lt;&lt; &quot;Number:&quot; &lt;&lt; setfill(' ') &lt;&lt; numStudents &lt;&lt; endl;
      cout &lt;&lt; setw(15) &lt;&lt; left &lt;&lt; &quot;Average:&quot; &lt;&lt; setfill(' ') &lt;&lt; avgNumGrade &lt;&lt; endl;
      fin.close();
  }
  </code></pre>