hii have a small problem here.
i have written a program ( it is supposed to be easy and simple) . it computes 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 in one line. The quiz scores are whole numbers and are separated by one space. the program will take its input from this file and send its output to a second file. The data in the output file will be exactly 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.
well, every thing worked fine except for the average. so, this is my code and thank you
note:
i only took the basics in C++ i took functions and files (I/O streams)... i didn't take strings,structures, classes, pointers, linked lists, or arrays..
Code:#include<iostream> #include<fstream> #include<cstdlib> using namespace std; void check ( ifstream& in_f, ofstream& out_f ); // This function checks if the files are successfully opened... void copy ( ifstream& in_file , ofstream& out_file ) ; // This function copies the information from the input file to the output file // as well as computing and writing the average for each student.... int main () { ifstream in_stream ; ofstream out_stream ; cout << " Hello *_* welcome to my program ... \n" ; check ( in_stream , out_stream ); copy ( in_stream , out_stream ); in_stream.close(); out_stream.close(); cout << " That's all for now *_* see you later \n"; return 0 ; } void check (ifstream& in_f, ofstream& out_f ) { in_f.open("in_file.dat") ; if ( in_f.fail() ) { cout << " Input file opening failed ...\n" ; exit (1) ; } out_f.open("out_file.dat"); if ( out_f.fail() ) { cout << " Output file opening failed ...\n" ; exit (1) ; } } void copy ( ifstream& input_file , ofstream& output_file ) { char symbol; int sum,count; double average ; while ( ! input_file.eof() ) { input_file.get(symbol); while ( ! isdigit(symbol) ) { output_file.put(symbol); input_file.get(symbol); } sum=0; count=1; while (symbol !=('\n')) { output_file.put(symbol); if (isspace(symbol)) count = count + 1 ; if (isdigit(symbol)) sum = sum + symbol - 48; input_file.get(symbol); } output_file<< " (** sum= "<< sum << ", count: " << count ; output_file.setf(ios::fixed); output_file.setf(ios::showpoint); output_file.precision(2); average = double(sum) / count ; output_file << ", average: "<< sum << " / " << count << " = " << average << " **) "<<endl<<endl ; }



LinkBack URL
About LinkBacks
i have a small problem here.
) . it computes 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 in one line. The quiz scores are whole numbers and are separated by one space. the program will take its input from this file and send its output to a second file. The data in the output file will be exactly 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. 


