patterncppMinor
Student Marking Calculator
Viewed 0 times
studentcalculatormarking
Problem
This is a student module marks calculator that I was asked to write for a uni revision session in year 2.
Is this a good example of modern C++ code? Is there anything I can improve on or need to change to become a better programmer?
Is this a good example of modern C++ code? Is there anything I can improve on or need to change to become a better programmer?
#include
#include
float student_results[5][2];
unsigned int input_student_result(unsigned int);
float input_student_weighting(unsigned int);
unsigned int input_exam_result();
float calculate_exam_weighting();
float calculate_module_mark();
int main() {
// Coursework 1
for (unsigned int i = 0; i = ";
std::cin >> input_validation;
} while (!(input_validation >= 1 && input_validation > input_validation;
} while (!(input_validation == 0.1f || input_validation == 0.2f));
return input_validation;
}
unsigned int input_exam_result() {
unsigned int input_validation = 0;
do {
std::cout = ";
std::cin >> input_validation;
} while (!(input_validation >= 1 && input_validation <= 100));
return input_validation;
}
float calculate_exam_weighting() {
float exam_weighting = (1 - (student_results[0][1] + student_results[1][1] + student_results[2][1] + student_results[3][1]));
std::cout << "Exam weighting = " << exam_weighting << '\n';
return exam_weighting;
}
float calculate_module_mark() {
float module_mark{
(student_results[0][0] * student_results[0][1]) +
(student_results[1][0] * student_results[1][1]) +
(student_results[2][0] * student_results[2][1]) +
(student_results[3][0] * student_results[3][1]) +
(student_results[4][0] * student_results[4][1])
};
return module_mark;
}Solution
Here is how you can improve your code:
By the way, you don't look like a beginner since you
This is already quite good.
- Define a class instead of using an array for unrelated numbers (0 to 100 vs. 0.1 or 0.2)
- Use a
std::vectorinstead of a fixed size array
- Prefer
doubleoverfloat, since it has higher precision
- Prefer integer arithmetic over floating point arithmetic, since it won't lead to strange results that easily
- Group your global functions into a class, to express that they belong together
- Add error checking for all calls to
std::cin >> var
By the way, you don't look like a beginner since you
- don't import the complete
stdnamespace
- use
\ninstead ofstd::endl
This is already quite good.
Context
StackExchange Code Review Q#144180, answer score: 2
Revisions (0)
No revisions yet.