title: c++ reading text file and output grades and average to another file (messed up output)
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8862058/enhancing-a-program-complete-failure">enhancing a program - complete failure</a>  </p>
  </blockquote>
  
  
  
  <p>im asked to write a program to read the content of a text file in the form of:</p>
  
  <pre><code>Jones Tom 94 99 96 74 56 33 65 89 87 85  
  Thompson Frank 67 58 86 95 47 86 79 64 76 45  
  Jackson Tom 95 97 94 87 67 84 99 45 99 87  
  Jackson Michael 43 23 34 77 64 35 89 56 75 85  
  Johnson Sara 84 93 64 57 89 99 74 64 75 35 91
  </code></pre>
  
  <p>and output the average of each student into another file in the form of:</p>
  
  <pre><code>Jones Tom 94 99 96 74 56 33 65 89 87 85 77.8
  Thompson Frank 67 58 86 95 47 86 79 64 76 45 70.3
  Jackson Tom 95 97 94 87 67 84 99 45 99 87 85.4
  Jackson Michael 43 23 34 77 64 35 89 56 75 85 58.1
  Johnson Sara 84 93 64 57 89 99 74 64 75 35 73.4
  </code></pre>
  
  <p>i successfully managed to do that using the code below:</p>
  
  <pre><code>#include &lt;iostream&gt;
      #include &lt;fstream&gt;
      #include &lt;cstdlib&gt;
      #include &lt;cstring&gt;
      using namespace std;
  
      int main()
      {
          fstream infile("grades.txt",ios::in);
          if(!infile){cerr&lt;&lt;"file could not be found!";exit(1);}
  
          fstream outfile("average.txt",ios::out);
          if(!outfile){cerr&lt;&lt;"file could not be created!";exit(1);}
  
  
          char fname[20];
          char lname[20];
          int grades;
          char c;
          int lines=1;
          double avg=0;
  
          while(infile.get(c))
          {if(c=='\n') lines++;}
          infile.clear();
          infile.seekg(0);
  
          for(int k=0;k&lt;lines;k++)
              {
                  infile&gt;&gt;fname;
                  infile&gt;&gt;lname;
                  outfile&lt;&lt;fname&lt;&lt;" "&lt;&lt;lname&lt;&lt;" ";
                  int sum=0;
                  for(int i=0;i&lt;10;i++)
                  {
                      if(infile&gt;&gt;grades)
                      {sum+=grades;
                      outfile&lt;&lt;grades&lt;&lt;" ";}
                  }
  
                  outfile&lt;&lt;(double)sum/10.0&lt;&lt;endl;
              }
  
  
          system("pause");
          return 0;
      }
  </code></pre>
  
  <p>but i have a problem.
  when the first line of the text file contains grades less than 10. for example:</p>
  
  <blockquote>
    <p>Jones Tom 94 99 96 74 56 33 65 89 87</p>
  </blockquote>
  
  <p>i get a messed up output, which is ruining everything. im getting the output below:</p>
  
  <pre><code>Jones Tom 94 99 96 74 56 33 65 89 87 69.3
    0
    0
    0
    0
  </code></pre>
  
  <p>How can i fix this problem? and how can i make the program to output zeros if i have less than ten grades on each line? 
  so that for example if i have on the first line:</p>
  
  <pre><code>Jones Tom 94 99 96 74 56 33 65 89
  </code></pre>
  
  <p>i want the program to calculate the average and output the following to the other file.</p>
  
  <pre><code>Jones Tom 94 99 96 74 56 33 65 89 0 0 64.3
  </code></pre>
  
  <p>note that 64.3 is the average of the student.</p>
  
  <p>thank you.
  kind regards.</p>