patterncppMinor
Calculating and generates a report card for a student
Viewed 0 times
generatesstudentcardcalculatingreportforand
Problem
I'm working on this small program for practice and would like to know how I could improve it from here. I'm still a relative beginner with C++ and would like to know if there is a better why to design this program.
Things I want to do:
-
Implement a while loop so that the program will continue to take input and write to the file until the user is done.
-
Use and
Thoughts? Other things I can implement to build on my current knowledge?
```
// Calculates and generates a report card of students final grades
#include
#include
using namespace std;
const float numOfAssignments = 4;
const float assignmentWeight = 0.4;
const float midtermWeight = 0.15;
const float finalWeight = 0.35;
const float participationWeight = 0.1;
float assignmentScore();
float midtermScore();
float finalScore();
float participationScore();
string student();
int main(){
float finalGrade, assignments, midterm, final, participation;
string name;
ofstream Grades;
Grades.open("finalgrade.txt");
name = student();
assignments = assignmentScore();
midterm = midtermScore();
final = finalScore();
participation = participationScore();
// Adds all scores together for final grade
finalGrade = assignments + midterm + final + participation;
// Check if there is a file to write to
if (Grades.is_open()){
Grades 70){
Grades 90){
Grades > assign1;
cout > assign2;
cout > assign3;
cout > assign4;
average = (assign1 + assign2 + assign3 + assign4) / numOfAssignments;
assignmentScore = average * assignmentWeight;
cout > midterm;
midtermScore = midterm * midtermWeight;
cout > final;
finalScore = final * finalWeight;
cout > participation;
participationScore = participation * participationWeight;
cout > first_name;
cout > last_name;
student = fir
Things I want to do:
-
Implement a while loop so that the program will continue to take input and write to the file until the user is done.
-
Use and
ifstream as a way to input the data instead of through the console and write the results to the output file.Thoughts? Other things I can implement to build on my current knowledge?
```
// Calculates and generates a report card of students final grades
#include
#include
using namespace std;
const float numOfAssignments = 4;
const float assignmentWeight = 0.4;
const float midtermWeight = 0.15;
const float finalWeight = 0.35;
const float participationWeight = 0.1;
float assignmentScore();
float midtermScore();
float finalScore();
float participationScore();
string student();
int main(){
float finalGrade, assignments, midterm, final, participation;
string name;
ofstream Grades;
Grades.open("finalgrade.txt");
name = student();
assignments = assignmentScore();
midterm = midtermScore();
final = finalScore();
participation = participationScore();
// Adds all scores together for final grade
finalGrade = assignments + midterm + final + participation;
// Check if there is a file to write to
if (Grades.is_open()){
Grades 70){
Grades 90){
Grades > assign1;
cout > assign2;
cout > assign3;
cout > assign4;
average = (assign1 + assign2 + assign3 + assign4) / numOfAssignments;
assignmentScore = average * assignmentWeight;
cout > midterm;
midtermScore = midterm * midtermWeight;
cout > final;
finalScore = final * finalWeight;
cout > participation;
participationScore = participation * participationWeight;
cout > first_name;
cout > last_name;
student = fir
Solution
Congratulations for using some constants, but you could use more in the following lines:
Here the maximum score is hardcoded (written directly inside the source code), I suggest making the max scores constants too.
Please aviod all those prototypes:
just move the main function at the end and you can delete them.
Please check for more errors in the UI, the following cases are not handled:
should be avoided.
You could write a loop and/or a function to remove some of the duplication from below:
Grades 70){Here the maximum score is hardcoded (written directly inside the source code), I suggest making the max scores constants too.
Please aviod all those prototypes:
float assignmentScore();
float midtermScore();
float finalScore();
float participationScore();
string student();just move the main function at the end and you can delete them.
Please check for more errors in the UI, the following cases are not handled:
- A name with spaces is entered (such names exist so you cannot crash on them).
- Nothing happens if I enter a negative or ridiculously big number, you should put some checks for that.
- If I enter a string instead of a number the program crashes, you should handle that and ask for input again.
using namespace std;should be avoided.
You could write a loop and/or a function to remove some of the duplication from below:
cout > assign1;
cout > assign2;
cout > assign3;
cout > assign4;Code Snippets
Grades << "Assignment score out of 40: " << assignments << endl;
Grades << "Midterm score out of 15: " << midterm << endl;
Grades << "Final Exam score out of 35: " << final << endl;
Grades << "Participation score out of 10: " << participation << endl;
Grades << "**************************************" << endl;
Grades << "The final grade is: " << finalGrade << endl;
if (finalGrade > 70){float assignmentScore();
float midtermScore();
float finalScore();
float participationScore();
string student();using namespace std;cout << "Enter the score for the first assignment: ";
cin >> assign1;
cout << "Enter the score for the second assignment: ";
cin >> assign2;
cout << "Enter the score for the third assignment: ";
cin >> assign3;
cout << "Enter the score for the fourth assignment: ";
cin >> assign4;Context
StackExchange Code Review Q#86178, answer score: 3
Revisions (0)
No revisions yet.