za stacka2345
za stacka2345

Reputation: 37

Comparing the best average grade from the file and print the student's name

I have a students.txt file:

Jim Heat 3 3 3.3
Joe Smith 4 4 3.7
...

As you can see the data are sorted in order: name, surname, class (1-4), grade, average grade. I have determined the total number of students in the file, the number of 4th grade students, the names of the students who have excellent results, but I still need to determine the name of the student who has the best average grade. I don't know how I will compare the average grades from the file and then print the student's name with the best average grade.

Here is my code:

#define length 20
struct student{
char name[20];
char surname[20];
int class;
int grade;
float average_grade;
};

int main()
{int s,br=0;
  ifstream input;
  input.open("students.txt");

if (input) {
    int number_of_students = 0;
    string line;
    while (getline(input, line))
      ++number_of_students;
    cout << "Total number of students: " << number_of_students <<endl;
  } else {
    cout << "Could not open the file"<<endl;
  } 

input.close();

input.open("students.txt"); 
int i=0,br1=0;
float pr;

struct student arr[length]; 

while(input>>arr[i].name>>arr[i].surname>>arr[i].class>>arr[i].grade>>arr[i].average_grade && i<length)
if (arr[i].class==4)
{
br++;   
}

cout<<"The number of fourth grader students "<<br<<endl;

input.close();

input.open("students.txt");
while(input>>arr[i].name>>arr[i].surname>>arr[i].class>>arr[i].grade>>arr[i].average_grade && i<length)
if(arr[i].grade>4)
{
cout<<"Student with excellent grade "<arr[i].name<<" "<<arr[i].surname<<endl;
}   

input.close();

return 0;
}

Upvotes: 0

Views: 512

Answers (2)

Thomas Matthews
Thomas Matthews

Reputation: 57729

I would overload operator>> to make input easier:

#define length 20
struct Student
{
    char  name[length];     // Should be std::string
    char  surname[length];  // Should be std::string
    int   class_id;         // Changed because "class" is a keyword.
    int   grade;
    float average_grade;

    friend std::istream& operator>>(std::istream& input, Student& s);
};

std::istream& operator>>(std::istream& input, Student& s)
{
    input >> s.name;
    input >> s.surname;
    input >> s.class_id;
    input >> s.grade;
    input >> s.average_grade;
    return input;
}

Now you can create two Student instances, one to read in and another to maintain the running "best" average grade:

Student s;
Student best_grade;
best_grade.average_grade = 0.0;
while (input >> s)
{
    if (s.average_grade > best_grade.average_grade)
    {
        best_grade = s;
    }
}

The student with the maximum average_grade is stored in the Student variable best_grade. After the loop, you can print the fields of best_grade.

Upvotes: 1

dewDevil
dewDevil

Reputation: 391

Just like you are iterating through the file line by line determining the other values like excellent students, similarly go through the file line by line to determine the max average grade by storing a max initially to the first element and then if you encounter any grade higher than that, store it as max and also storing the index of that average grade where it is found so that you can output the name corresponding to that grade. Update the index accordingly when you get any grade higher than before and then the final index that you will be storing will be for max average grade and corresponding name of student.

Upvotes: 0

Related Questions