### Program to compute students' grades

I am having some trouble with my program as I need to be able to input an exam score and 7 test scores for a student to determine their final score and final letter grade. As of now I can input the 7 test scores but only by inputting them one at a time. I need to be able to input 7 test scores all at once and have the function read them in a loop and compute their average.

Also in main how can I output the comment for the corresponding grade. As of now it outputs as:
COMMENT:
Satisfactory
but I need them in the same line.

Here is what I have so far:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````#include using namespace std; void readTestScores(double & exam, double & tavge) { // Read the student's exam score cout << endl << "ENTER EXAM SCORE: "; cin >> exam; // Print prompt and read the student's test scores int sum = 0; int score[7]; int i; for(i = 0; i < 7; i++) { cout << endl << "ENTER ALL TEST SCORES: "; cin >> score[i]; sum = sum + score[i]; } tavge = sum / 7; } double computeFinalScore(double exam, double tavge) { // Computes final score as follows: 4/10(test scores average) + 6/10(exam score), then returns final score double finalscore; finalscore = (.4 * tavge) + (.6 * exam); return finalscore; } char getLetterGrade(double score) { char grade; if(score >= 90) grade = 'A'; else if(score >= 80 && score < 90) grade = 'B'; else if(score >= 70 && score < 80) grade = 'C'; else if(score >= 60 && score < 70) grade = 'D'; else if(score < 60) grade = 'F'; return grade; } void printComment(char grade) { switch(grade) { case 'A': cout << endl << "Very Good"; break; case 'B': cout << endl << "Good"; break; case 'C': cout << endl << "Satisfactory"; break; case 'D': cout << endl << "Need Improvement"; break; case 'F': cout << endl << "Poor"; break; }; } int main() { int id; double exam; double tavge; double finalscore; char grade; while(1) { cout << endl << "ENTER STUDENT ID: "; cin >> id; readTestScores(exam, tavge); finalscore = computeFinalScore(exam, tavge); cout << endl << "TEST AVERAGE IS: " << tavge; cout << endl << "FINAL SCORE IS: " << finalscore; grade = getLetterGrade(finalscore); cout << endl << "LETTER GRADE IS: " << grade; cout << endl << "COMMENT: "; printComment(grade); // cout << endl << "Comment: " << comment; } return(0); }``````
Last edited on
Comment and the actual comment are not in the same line because of the <<endl before the actual comment in your printcomment(), placing it after the comment might be a better idea
Hello ac829,

In the "readTestScores" function you could do your input as `cin >> score[0] >> score[1] >> score[2] . . .`.

You could do "sum" the same way or leave it in the for loop.

You might also consider defining the array in main and passing it to the functions that could use it. Unless it is only needed in this one function.

All of your variables in main are uninitialized, so on line 85 you are sending two of these variables to a function. Even though they are passed by reference there could still be a problem.

Right off I do not see any problem with your code for printing the comment, but it will be about an hour before I can test it.

Hope that helps,

Andy
Handy Andy

Appreciate the help. For the comment I tried

cout << endl << "COMMENT: : << printComment(grade);

to try and get it to print "COMMENT: satisfactory" but I get an error. The way I have it now it prints the actual comment on the next line after "COMMENT:"
I was referring to this potion, try this:

 ``123456789101112131415161718192021`` ``````void printComment(char grade) { switch(grade) { case 'A': cout << "Very Good" << endl; break; case 'B': cout << "Good" << endl; break; case 'C': cout << "Satisfactory" << endl; break; case 'D': cout << "Need Improvement" <

Then for printing:

 ``12`` `````` cout << "COMMENT: "; printComment(grade);``````

It's better to put your line termination at the end of a line as opposed to starting a new line with one.

ohh I misunderstood and changed the line termination in main instead of the printComment function. It works perfectly now. Thanks a lot
Andy,

Is it possible to use a while loop instead of a for loop for reading the 7 test scores?
Although I'm not Andy, Any for loop can be implemented as a while loop you wil just need to keep track yourself. Both of the following loops do the same thing.

 ``1234567891011`` ``````for(int i=0; i < 10; i++) { std::cout << i << std::endl; } int count = 0; while(count < 10) { std::cout << i << std::endl; count++; }``````
Hello ac829,

As knowclue said yes you can use a while loop and his example should work for what you want.

As I worked on the program I realized more of the problems that are there.

To start with it is a good practice and habit to initialize your variables when you define them. I had some problems with "tavge" and "finalscore" until I initialized the variables. Also that is when I discovered that in the "readTestScores" function "sum" and "score" were defined as "int"s when they should be a double. Then the numbers started to come out better.

Consider this:
 ``123456789101112131415161718192021222324`` ``````void readTestScores(double & exam, double & tavge) { // Read the student's exam score cout << endl << "ENTER EXAM SCORE: "; cin >> exam; // Print prompt and read the student's test scores double sum{}; double score[7]{}; int i; cout << endl << "ENTER ALL SEVEN TEST SCORES:\n\n"; //cin >> score[0] >> score[1] >> score[2] >> score[3] >> score[4] >> score[5] >> score[6]; for (i = 0; i < 7; i++) { std::cout << "Enter test score #" << i + 1 << ": "; std::cin >> score[i]; sum = sum + score[i]; } tavge = sum / 7.0; // <--- Changed to 7.0. }``````

Line 14 is one way of doing input, but after I thought about the for loop works better because it prompts for each score. Notice the change I made in line 13. Working with this function is when I noticed that "sum" and "score" had the wrong type. You were storing a floating point number as an int thus loosing everything to the right of the decimal point, so in the end "tavge" had the wrong answer.

The next two functions I have not looked at closely because they appear to work.

In main again initialize your variables when defined. The simplest way is `int id{};`. The empty {} will set "id" to zero. Or if you need something different just put the number inside the {}.

The while loop is an endless loop with no way out at this time. For now I commented out the while loop for testing.

Other than the use of "endl" being in the wrong place main works well. I did add this line to main:
`std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Requires header file "<iomanip>". ` This will affect the output of floating point numbers.

Just as a note the use of "endl" is a bit overused in your program. Many places in your "cout" statements you can use "\n" and use "endl" on the last line printed or use "std::flush()" if you feel the need.

With the proper corrections the program will work the way you want.

Hope that helps,

Andy
Topic archived. No new replies allowed.