changed the function but still !!!!
hello :D i used another way in the copy function and i asked my teacher to use this way and she said it is ok... The problem is ( i think ) how to move from one line to another after copying it from the input file and adding the average to it???? this is my function i know it looks stupid but what can i do???
Code:
void copy ( ifstream& input_file , ofstream& output_file )
{
char first_name[20] , last_name[20] ;
int sum,num1,num2,num3,num4,num5 ;
int num6,num7,num8,num9,num10;
double average ;
while ( ! input_file.eof() )
{
input_file >> last_name ;
output_file << " " << last_name ;
input_file >> first_name ;
output_file << " " << first_name ;
input_file >> num1 ; output_file << " " << num1 ;
input_file >> num2 ; output_file << " " << num2 ;
input_file >> num3 ; output_file << " " << num3 ;
input_file >> num4 ; output_file << " " << num4 ;
input_file >> num5 ; output_file << " " << num5 ;
input_file >> num6 ; output_file << " " << num6 ;
input_file >> num7 ; output_file << " " << num7 ;
input_file >> num8 ; output_file << " " << num8 ;
input_file >> num9 ; output_file << " " << num9 ;
input_file >> num10 ; output_file << " " << num10 ;
output_file.setf(ios::fixed);
output_file.setf(ios::showpoint);
output_file.precision(2);
sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
average = double(sum) / 10 ;
output_file << " Average = " << average << endl << endl ;
}
}
Re: changed the function but still !!!!
Code:
while()
{
//deal with names for this line
>> last name
<< last name
>> first name
<< first name
//deal with scores for this line
for()
{
>> score
<< score
total += score
}
calculate average for this line
<< average
}//go to next line
because >> ignores whitespace such as space character and newline character you will automatically go to the next line once you have read everything in from this line. This technique works great when you know that each line has exactly the same number of fields. It doesn't always work under other conditions however.
NOTE: if you use eof() as the terminating condition for the while loop in this psuedocode there will likely be a duplication of the last line of the input file in the output file. This bug is handled extensively on the board under other posts. Do a search using file handling, or eof() as the search term and I'm sure you will find some of them.