How can I improve this beginner C++ student management program?

0
5
Asked By MellowPine42 On

I'm a beginner programmer and built this basic student management program in C++. It stores student names, class names, and grades in a vector, then sorts the students, clamps grades to the 0–10 range, determines whether each student passed, and displays the average, highest grade, and lowest grade. I wrote it on my phone, so I wasn't able to use text files for persistent storage. I'd appreciate feedback on the code, style, correctness, and ideas for improving the design.

4 Answers

Answered By CodeHarbor7 On

The sorting function has a syntax problem. 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; });`. Without those parts, the program won’t compile. Also, use clearer names such as `students` rather than capitalizing a variable name like `Students`.

Answered By QuietMaple_8 On

A few general improvements would make this cleaner. Prefer `std::` instead of `using namespace std;`, since importing an entire namespace can cause name conflicts as programs grow. You can also use a constructor initializer list: `Student(std::string n, std::string cn, double g) : name(n), className(cn), grade(g) {}`. Avoid abbreviations where possible, and use `n` instead of `std::endl` when you only need a line break—`std::endl` also flushes the output stream and is usually unnecessary here.

BrightCedar19 -

The namespace issue becomes especially confusing when two libraries provide functions with the same name. Explicit `std::` prefixes make it clearer which function is being called and make those errors easier to diagnose.

Answered By NovaPebble31 On

There’s also a logic issue with when you sort and clamp the grades. You sort the vector before changing values outside the 0–10 range, so modifying a grade afterward can leave the displayed order incorrect. Validate or clamp each grade before sorting. You could also calculate the best and worst students with `std::max_element` and `std::min_element`, which would make the intent clearer.

Answered By SilverKite_56 On

The class works for a small exercise, but as the program grows, consider making the data members private and exposing small member functions when needed. A constructor can take `const std::string&` parameters, and a `const` qualifier on `isPassing()` would indicate that checking a student does not modify it. Later, you could add input handling and save the vector to a file so the records persist between runs.

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.