Loops and inFile

Hey guys, I basically just need a push in the right direction.

I need to create a program that reads sex and grades of students from a file, and calculates the average of the male and the female students seperately. I was thinking of doing it the old way

1
2
3
4
ifstream inFile;
inFile.open("grades.txt");
inFile>>sex1,grade1,sex2,grade2;
average_male=grade1+grade2/2;

etc...

However, there are 100 names in the list and it says I need to use a loop. The user is supposed to enter how many names they want from the list then the program counts the grades and finds the averages, etc.

I don't get what variable I should use for i ? Should there be more than one loop ? How do I use inFile inside of a loop?

Cheers,
Stridexr
Is there only one grade for each student?
Anyway, in a loop you do the same as outside it:
1
2
3
4
5
6
7
8
9
10
11
//open a file 'inFile'
//as for the number of students 'N'
for(int i = 0; i < N; i++){
   string sex;
   int grade;

   inFile >> sex >> grade;

   if(sex == "male")//... note that here you should calculate the sum of all grades and the number of students
   else//           calculate the averages after the loop.
}
Topic archived. No new replies allowed.