Wrong Final Grade

I'm trying to complete my assignment in which I have to calculate a students final grade. The code compiles but no matter the grades that I enter the answer is always the same. A letter grade of "F". Can someone tell me where I went wrong.

The Assignment :

For each student the program will read in a student’s name. It should also read in 10 test scores for the student and calculate his or her average. You must read the scores using a loop.
The program should output the student’s name, average and letter grade. The average should be displayed accurate to 1 decimal place.
Letter grades are assigned based on the scale:
90-100 A
80-89 B
70-79 C
60-69 D
<60 F

[/code]

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
string name;
int Numstudents,test,studentavg;
char grade;
double score;
cout <<" Enter number of students: ";
cin >> Numstudents;
cout << endl;

cout << " Enter student's name: ";
cin >> name;


double counter = 0;
while ( counter < Numstudents)
{
for ( int test =1; test <=10; test++)
{

cout << " Enter student's " << test << " grade";
cin >> score;
counter += score;
}
studentavg = score / 10;

if (studentavg >=90)
grade = 'A';
else if (studentavg >=80)
grade = 'B';
else if (studentavg >=70)
grade = 'C';
else if (studentavg >=60)
grade = 'D';
else
grade = 'F';

cout << " Student name: " << name<< endl;
cout << " Average Grade" << studentavg<< endl;
cout << "Letter Grade: " << grade<< endl;
counter++;
}
return 0;
}


[/code]
Last edited on

studentavg = score / 10;

will only use the last entered score, and even if they score 100%, dividing this by ten gives the average score of 10, thus a grade of 'F'

Also, you divide the last entered 'score' by 10, should you be dividing 'counter' by 10?

(btw - counter is not a good variable name - testScoreTotal might be better)
Hello DJM34,

Not sure where you went wrong with your code tags, but this might help
http://www.cplusplus.com/articles/z13hAqkS/

If this is an assignment for school please post the instructions that you were given, This will help everyone know what you have to do without having to guess.

The first thing I see is that you should initialize you variables when defined. It is always nice to know that they do not contain a garbage value.

This code does not make any sense.
1
2
3
4
5
6
cout << " Enter number of students: ";
cin >> Numstudents;
cout << endl;

cout << " Enter student's name: ";
cin >> name;

If you enter 5 for "numStudents" you are saying the you are only using 1 name for all 5 students. Or am I misunderstanding this?

In the while loop: while (counter < Numstudents) first time through this works. Once inside the loop first you have counter += score; then at the ens of the loop you have counter++;.

Next check of the while condition "counter" will most likely be greater than 5 and cause the condition to fail. When you say counter += score; did you mean to use a different variable?

In the for loop for (int test = 1; test <= 10; test++) the 10 should be "numStudents" because this could be less than 10 or greater.

The line studentavg = score / 10;. "studentavg" is defined as an int, but "score" is defined as a "double" the 10 is considered an "int", but will be promoted to a "double" for the calculation. The you are trying to stuff a "double" into an "int" which will store the whole number and drop the decimal part. not what you want. Just be the name "studentavg" one would think that this would be a "double" unless your intent is to use "int"s.

I have not run the program yet, but I think that the if/else if statements will work.

Andy
Hello DJM34,

After running the program I have questions.

To start with cin >> name; would be better as std::getline(std::cin, name);. If someone would enter (First Last) only First would be stored in "name" using the formatted input (cin >> name) and the rest would be left in the input buffer for the next "cin >>" to extract, which would mess up the program.

In your original for loop:
1
2
3
4
5
6
7
for (int test = 1; test <= 10; test++)
{
    cout << " Enter student's " << test << " grade: ";  // <--- Changed last string.
    cin >> score;

    counter += score;
}

Does this mean that each student will have 10 grades to average? And "counter" is the wrong variable to use here.

Andy
Okay so the assignment requires 10 "grades" to be inputted and calculated
Hello DJM34,

You might start with something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <limits>  // <--- Added.

using namespace std;

int main()
{
    constexpr double NUM_OF_SCORES{ 5.0 };  // <--- Reduced for testing. Change to 10 latar.

    string name;
    int Numstudents{}, test{};  // <--- Good idea to initialize your variables.
    char grade{};
    double score{}, studentavg{};

    std::cout << std::fixed << std::setprecision(4);

    cout << " Enter number of students: ";
    cin >> Numstudents;
    cout << endl;

    // <--- Clears the input buffer. Needed before the "getline()".
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

    cout << " Enter student's name: ";
    std::getline(std::cin, name);

    int counter{};
    double totalScore{};  // <--- Added.

    while (counter < Numstudents)
    {
        for (int test = 1; test <= NUM_OF_SCORES; test++)
        {
            cout << " Enter student's " << test << " grade: ";
            cin >> score;

            totalScore += score;  // <--- Changed.
        }

        studentavg = totalScore / NUM_OF_SCORES;

        if (studentavg >= 90.0)

//  The rest should be changed. 


Andy
@DJM34

Unless you want to get bogged down, here are the few simple changes you need - total_score was the problem.

As a suggestion for testing purposes just use say 3 tests instead of 10 then change it back to 10 when you are finished and want to do a final check. Save typing ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cmath> // DON'T NEED
#include <string>
#include <iomanip> // HARDLY NEEDED, JUST USE SPACES HERE AND THERE

using namespace std;

int main()
{
    string name;
    int Numstudents = 0,  test = 0, studentavg = 0;
    char grade = '?';
    double score = 0;
    cout <<" Enter number of students: ";
    cin >> Numstudents;
    cout << endl;
    
    cout << " Enter student's name: ";
    cin >> name;
    
    
    double counter = 0;
    double total_score{0};
    
    while ( counter < Numstudents)
    {
        total_score = 0;
        for ( int test =1; test <=10; test++)
        {
            
            cout << " Enter student's " << test << " grade";
            cin >> score;
            total_score += score;
        }
        studentavg = total_score / 10;
        
        if (studentavg >=90)
            grade = 'A';
        else if (studentavg >=80)
            grade = 'B';
        else if (studentavg >=70)
            grade = 'C';
        else if (studentavg >=60)
            grade = 'D';
        else
            grade = 'F';
        
        cout << " Student name: " << name << endl;
        cout << " Average Grade" << studentavg<< endl;
        cout << "Letter Grade: " << grade << endl;
        counter++;
    }
    return 0;
}



Enter number of students: 1

 Enter student's name: Alan
 Enter student's 1 grade50
 Enter student's 2 grade60
 Enter student's 3 grade90
 Enter student's 4 grade87
 Enter student's 5 grade66
 Enter student's 6 grade55
 Enter student's 7 grade45
 Enter student's 8 grade87
 Enter student's 9 grade12
 Enter student's 10 grade98
 Student name: Alan
 Average Grade65
Letter Grade: D
Program ended with exit code: 0
Thank you, everyone for replies and help. I used againtry's code as it best fitted the assignment.
Topic archived. No new replies allowed.