Beginner C++ Student Management Program—How Can I Improve It?

0
6
Asked By MellowCactus42 On

I'm a beginner programmer and wrote this small student management program in C++. It stores students in a vector, sorts them by grade, clamps grades to the 0–10 range, determines whether each student passed, and prints the average, highest grade, and lowest grade. I coded it on my phone, so I wasn't able to use text files for persistent storage. I'd appreciate feedback on the design, style, correctness, and ways to improve the program.

3 Answers

Answered By QuietMaple31 On

You can simplify the constructor with a member-initializer list: `Student(std::string n, std::string cn, double g) : name(std::move(n)), className(std::move(cn)), grade(g) {}`. It initializes the members directly instead of default-constructing them and assigning afterward. It would also be clearer to use full variable names consistently rather than abbreviations.

Answered By BrightOtter7 On

The sorting function won’t compile as written. The comparator needs both a capture list and parameters, for example: `std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) { return a.grade > b.grade; });`. Also, consider using `std::` explicitly instead of `using namespace std;`. That avoids name collisions when different libraries contain functions with the same name.

PixelHarbor19 -

Name collisions can be especially confusing for beginners because the selected function depends on which names have been brought into scope. Using `std::` makes the source of each type or function clear.

Answered By CopperFalcon8 On

Prefer `n` over `std::endl` for ordinary output. `std::endl` inserts a newline and flushes the stream, which is usually unnecessary and can be slower. A few other improvements are worth considering too: make `isPassing()` `const`, keep the members private, validate or clamp grades before sorting, and handle an empty student vector before accessing `students[0]` or dividing by `students.size()`.

NovaPebble56 -

Clamping after sorting can leave the vector no longer correctly sorted, so normalize the grades before calling the sort function if sorted output is important.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.