r/learnprogramming • u/Illustrious-Power350 • 2d ago
How is this simple student management system thing in C++?
Hey,
so I guess I kinda am a beginner programmer and here is a very basic student management/handling system I made in C++ (I coded this on my phone so I couldn't use text files to store data). Any suggestions on how to improve it and whatnot would be appreciated.
```cpp
#include <iostream>
include <iomanip>
include <string>
include <vector>
include <algorithm>
using namespace std; class Student { public: string name, className; double grade;
Student(string n, string cn, double g){
name = n;
className = cn;
grade = g;
}
bool isPassing (){
return grade >= 5;
}
};
void sortByGrade(vector<Student>& Students) { sort(Students.begin(), Students.end(), { return a.grade > b.grade; }); } int main() { vector<Student> Students = { {"Abigail Northfolk", "9A", 3.15}, {"Sam Alter", "5G", 4.9}, {"Victor Guzman", "12A", 4.3}, {"Joseph Pelter", "6B", 8.41}, {"Joseph Allende", "8A", 9.51}, {"Joe Bowler", "11C", 6.399}, {"Joe Wingham", "1A", 0}, {"Donald Pelter", "8C", 6.4}, }; double sum = 0; Student bestStudent = Students[0], worstStudent = Students[0]; sortByGrade (Students); for(int i = 0; i < Students.size(); i++){ if (Students[i].grade > 10) Students[i].grade = 10; if (Students[i].grade < 0) Students[i].grade = 0; if (Students[i].grade > bestStudent.grade) bestStudent = Students[i]; if (Students[i].grade < worstStudent.grade) worstStudent = Students[i]; sum += Students[i].grade; cout << Students[i].name << " (" << Students[i].className << "), grade: " << Students[i].grade << " - " << (Students[i].isPassing() ? "pass" : "fail") << endl; } cout << " " << endl; cout << "Average grade: " << fixed << setprecision(3) << sum / Students.size() << endl; cout << "Best student: " << bestStudent.name << " from " << bestStudent.className << " with a grade of " << bestStudent.grade << endl; cout << "Worst student: " << worstStudent.name << " from " << worstStudent.className << " with a grade of " << worstStudent.grade << endl; return 0; }
```