Sunday, July 12, 2009

C++ file I/O question?

My problem is I have to compute an average of grades from students from a text file and put the original data plus the averages in a new file. Each line of the input file contains a student's last name, a space, the student's first name, a space, and then ten quiz scores (whole numbers) separated by spaces all on one line. The data of the input file will be exactly the same as the input file, except there will be an average at the end of each line.





I have all the general open input file, output file, etc. stuff...I just don't know how to begin on the rest of it.

C++ file I/O question?
#include %26lt; fstream%26gt;





int main()


{





//open the file


ifstream in;


in.open("file.txt");





//open the output file


ofstream out;


out.open("out.txt");





//declaration


string firstname;


string lastname;


double score;


double sum = 0;


int n =1;





while (!in.eof())


//while it is not the end of file


{


// get the last name, first name and score in each line


in %26gt;%26gt; lastname %26gt;%26gt; firstname %26gt;%26gt; score;





//print in the output file the last name, first name and score


out %26lt;%26lt; lastname %26lt;%26lt; " " %26lt;%26lt; firstname %26lt;%26lt; " " %26lt;%26lt;score %26lt;%26lt; " ";





//print the average


sum = sum + score;


out %26lt;%26lt; sum / n %26lt;%26lt; endl;


n++;


}





//close the file


in.close()


out.close();





}// end of main


No comments:

Post a Comment