title: enhancing a program - complete failure
body: <p>good day everyone. 
  im having some trouble trying to figure out how to enhance my program.</p>
  
  <p>here's the question:</p>
  
  <p>Write a program to compute numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the following format: Each line contains a student’s last name, then one space, then the student’s first name, then one space, then ten quiz scores all on one line. The quiz scores are whole numbers and are separated by one space. Your program will take its input from this file and send its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student’s ten quiz scores. If this is being done as a class assignment, obtain the file names from your instructor. Use at least one function that has file streams as all or some of its arguments.</p>
  
  <p>i managed to do the first part successfully. below is the code:</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[10];
      char c;
      int x;
      cout&lt;&lt;"how many students?";
      cin&gt;&gt;x;
  
          for(int k=0;k&lt;x;k++)
          {
              infile&gt;&gt;fname;
              infile&gt;&gt;lname;
              for(int i=0;i&lt;10;i++)
              infile&gt;&gt;grades[i];
              outfile&lt;&lt;fname&lt;&lt;" "&lt;&lt;lname&lt;&lt;" ";
              double sum=0;
              for(int j=0;j&lt;10;j++) 
              {
  
                  outfile&lt;&lt;grades[j]&lt;&lt;" ";
                  sum+=grades[j];
  
              }
  
              double avg=0;
              avg=sum/10;
              outfile&lt;&lt;avg&lt;&lt;endl;
          }
  
  
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>im not able to do part (a) of the second part. i tried initializing the grades[10] array to zeros, but im not getting any correct output. any help? thank you.</p>
  
  <blockquote>
    <p>Enhance the program you wrote for (Problem 10) in all the following
    ways.  </p>
    
    <p>a-The list of quiz scores on each line will contain ten of fewer quiz
    scores. (If there are fewer than ten quiz       scores, that means
    that the student missed one or more quizzes.) The average score is
    still the sum of the      quiz scores divided by 10. This amounts to
    giving the student a 0 for any missed quiz. </p>
    
    <p>b-The output file will contain a line (or lines) at the beginning of
    the file explaining the output. Use     formatting instructions to
    make the layout neat and easy to read. c- After placing the desired
    output in an output file, your program will close all files and then
    copy the     contents of the “output” file to the  “input”  file so
    that the net effect is to change the contents of the input     file.<br>
    Use at least two functions that have file streams as all or some of
    their arguments. If this is being done as a    class assignment,
    obtain the file names from your instruction.</p>
  </blockquote>
  
  <hr>
  
  <p>here's how my code looks now</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;
      int sum=0;
      int linecount=0;
      char c;
  
      while(!infile.eof())
      {
              infile&gt;&gt;lname;
              infile&gt;&gt;fname;
              outfile&lt;&lt;lname&lt;&lt;" "&lt;&lt;fname&lt;&lt;" ";
              for(int i=0;i&lt;10;i++){if(infile&gt;&gt;grades)outfile&lt;&lt;grades&lt;&lt;" ";else {outfile&lt;&lt;"0 ";break;} sum+=grades;}
              outfile&lt;&lt;double(sum/10.0);
      }
              system("pause");
              return 0;
  }
  </code></pre>
  
  <p>but im getting just a black space when i run the program. im not able to fix the loop to read from all the lines of the file.</p>