3 cpp functions regarding infile outfile

I had an assignment where i had to write 5 functions and then run them all in a main function.
I can succesfully compile the first two and they work but there is something wrong with each of these last three and i really can't figure it out. any help would be awesome. thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void average(char infile[]) 
{ 
int number; 
double midterm_score, final_score, sum = 0, average; 
string name; 
istream fin; 
fin.open(infile); 
fin>>count; 
for(int i = 1; i<=count; ++i) 
{ 
fin>>name>>midterm_score>>final_score; 
sum = sum + midterm_score + final_score; 
average = sum/i; 
cout<<name<<average; 
} 
fin.close(); 
} 

void maximum(char infile[]) 
{ 
double maximum=0; 
double score; 
string name; 
ifstream fin(infile); 
fin>>name>>score; 
while (!fin.eof()) 
{ 
if (score>=maximum) 
cout<<name<<maximum = score; 
fin>>name>>score; 
} 
fin.close(); 
cout<<name<<"has the maximum score of: "<<maximum<<endl; 
} 

---------------------------------------... 

void grades(char infile[], char outfile[]) 
{ 
double score; 
char grade; 
string name; 
ifstream fin(infile); 
ofstream fout(outfile); 
fin>>name>>score; 
while(!fin.eof()) 
{ 
if (score>=50) 
grade = 'P'; 
else 
grade = 'F'; 

fout<<name<<grade; 
} 
fin.close(); 
fout.close(); 
}
Ok, a quick glance revealed the following :
1) There's a typo in the 6th line of your 1st function average(you wrote istream instead of ifstream).
2) You didn't declare count before using it.

Last edited on
I can succesfully compile the first two and they work but there is something wrong with ...

Your compiler is your friend. It will tell where and what it does not understand in your code. To understand compiler error messages is a useful skill to learn.

Compiler can be told to warn about legal but suspicious code too.


1
2
3
4
5
6
7
8
9
10
11
12
fin>>name>>score; // read once

while( !fin.eof() ) 
  {
    if (score>=50) 
      grade = 'P'; 
    else 
      grade = 'F'; 
    fout<<name<<grade;
    // fin did not change in this body
    // thus the end condition of this loop will never change 
  }
Topic archived. No new replies allowed.