Program to process test scores

I am writing a program to read the test scores of ten students into an array and then print the test scores with the corresponding letter grade and comment.

I think the for loop I have in main should be in the function readTestScores but it is not running correctly when I have it there.

This is what I've done so far:
(Some of the code is commented out so I could try running the program without any errors)

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
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;


int main()
{
    void readTestScores(double scoreList[], int size);
    //printTestResults(testList[], size);
}

void readTestScores(double scoreList[], int size)
{
    int n;
    
    for(n = 0; n < 10; n++)
    {
        cout << "Enter Test Score: ";
        cin >> scoreList[n];
    }
}

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 << "Very Good" << endl;
            break;
        case 'B':
            cout << "Good" << endl;
            break;
        case 'C':
            cout << "Satisfactory" << endl;
            break;
        case 'D':
            cout << "Need Improvement" << endl;
            break;
        case 'F':
            cout << "Poor" << endl;
            break;
    };
}

/*void printTestResults(double[] testList, int size)
{
    
}*/
Last edited on
closed account (SECMoG1T)
what problems are you having?
Does the for loop go in main or the function readTestScores()?

The instruction for #2 is: function void readTestScores(double scoreList[], int size) that receives an array to hold students' test scores and an integer value n and then reads n test scores into that array.
closed account (SECMoG1T)
for you to read n variables your function definitely need a loop there, your main would just call
the function once with the array passed as an argument
Hello ac829,

The first thing I see is line 21. This is a function proto typ not a function call, The function call would be readTestScores(scoreList, size);.

In line 22 loose the []s and it should work.

And line 23 should work as is.

Line 4 is OK, but you do not need the 10 for a 1D array. Also you will need proto types for the other functions after like 4.

Hope that helps,

Andy
How do I call the function readTestScores in main? The program ends without doing anything
Last edited on
closed account (SECMoG1T)
as @Andy said you need your protos before main
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

void readTestScores(double scoreList[], int size);
char getLetterGrade(double score);
void printComment(char grade)

int main()
{
    ///1. create an array of n elements here and a variable N 
    ///2. call readTestScores with the array  readTestScores(array,N);
   ///3. all other functions are called within printTestResults(double [] testList, int size)
}

void readTestScores(double scoreList[], int size)
{
    int n;
    
    for(n = 0; n < 10; n++)
    {
        cout << "Enter Test Score: ";
        cin >> scoreList[n];
    }
}

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 << "Very Good" << endl;
            break;
        case 'B':
            cout << "Good" << endl;
            break;
        case 'C':
            cout << "Satisfactory" << endl;
            break;
        case 'D':
            cout << "Need Improvement" << endl;
            break;
        case 'F':
            cout << "Poor" << endl;
            break;
    };
}

/*void printTestResults(double[] testList, int size)
{
    
}*/
Last edited on
Hello ac829,

Do not edit your original post like you did. now the code is completely different and the answers do not match.

How do I call the function readTestScores in main? The program ends without doing anything

Already showed you that in the second sentence of my last post.

You were also calling "printTestResults" with the wrong array from main.

I would also suggest to initial all your variables and arrays when they are defined.

You may consider using the "testList" array to store the letter grades derived from the "scoreList" array. Also renaming the variable would be good.

The first for loop is not needed when you get the "readTestScores" function working.

The line "grade = getLetterGrade(score);" would work better inside the "printTestResults" function. As it is in main it does not do much.

Hope that helps,

Andy
Last edited on
Sorry hadn't refreshed before I posted again.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std;

void readTestScores(double scoreList[], int size);
char getLetterGrade(double score);
void printComment(char grade);
void printTestResults(double testList[], int size);

int main()
{
    int size;
    double scoreList[]{};
    double testList[]{};
    //double score{};
    //char grade{};
    
    
    readTestScores(scoreList, size);
    printTestResults(testList, size);
    
}

void readTestScores(double scoreList[], int size)
{
    int n;
    
    for(n = 0; n < 10; n++)
    {
        cout << "Enter Test Score " << n+1 << ": ";
        cin >> scoreList[n];
    }
}

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 << "Very Good" << endl;
            break;
        case 'B':
            cout << "Good" << endl;
            break;
        case 'C':
            cout << "Satisfactory" << endl;
            break;
        case 'D':
            cout << "Need Improvement" << endl;
            break;
        case 'F':
            cout << "Poor" << endl;
            break;
    };
}

void printTestResults(double testList[], int size)
{
    char grade;
    //grade = getLetterGrade();
    /cout << "Letter Grade: " << grade;
    //printComment(grade);
}


After entering in the 10 test scores, I am getting an error for bad access at the end of the main function.

How can I pass the scores from array scoreList to function printTestResults() to print the test scores with the corresponding letter grades and comments?
You need to supply sizes for your arrays, using empty brackets will probably not produce the expected outcome.

Also note that in C++ array sizes must be compile time constants.

1
2
3
4
5
int main()
{
    int size;
    double scoreList[]{};
    double testList[]{};


1
2
3
4
5
void readTestScores(double scoreList[], int size)
{
    int n;
    
    for(n = 0; n < 10; n++)

What is the purpose of that size parameter?

Why the magic number 10? Wouldn't it make more sense to use that size parameter (once you supply the correct value to it in main()).
closed account (SECMoG1T)
in addation, i see you only need one array not two
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double scoreList[]{};///initialize size as adviced above

///your calls in main should be like
readTestScores(scoreList, size); ///where size is already defined above
printTestResults(scoreList, size);

///define your print score function properly here are some pointers

///1.the function should use a loop to iterate through the array upto size times
///2.on each iteration call the func getchargrade passing it the value  stored
///   on that index, store the return value in a variable.
//    -  use that variable to call getcomment and store the result
//     - print the three values on one line.
//       score ----  chargrade ----- comment
//  3.continue the loop till the end of the array 
Last edited on
Hello ac829,

On line 11 you define "size", but never give it a value or initialize it, so it contains garbag. Then on line 18 you send that garbage to the "readTestScores" function. Luckily you do not use "size" in the function so it does not matter at this point. You also pass "size" to the "printTestResults" function, but never use it.

The "printTestResults" function has potential, but does not work with what you have. The function receives two parameters, but neither are used. The function should step through the array. In the 'cout" statement you could call the "getLetterGrade" and "printComment" functions to not only print the numerical score from the array, but use the array for "getLetterGrade" and use "getLetterGrade" for the comment. One possible solution:

1
2
3
4
5
6
7
8
void printTestResults(double testList[])
{
	for (size_t lc = 0; lc < 10; lc++)
	{
		cout << "Test score: " << testList[lc] << " - " << getLetterGrade(testList[lc]) << " ";
		printComment(getLetterGrade(testList[lc]));
	}
}


When I compiled the program I found problems at the beginning of main. The "scoreList" array did not have a size inside the []. The same for "testList" which is never used.

Line 19 is using the wrong array. "testList" is empty and never receives any values.

I changed "size" to constexpr int SiZE{ 10 }; and then used it in lines 12 and 13 between the []s.

My beginning of main looks like this:

1
2
3
4
5
6
7
int main()
{
	mstd::SetScreen();

	constexpr int SIZE{ 10 };
	double scoreList[SIZE]{ 90, 80, 60, 26, 91, 81, 75, 86, 95, 100 };  // <--- Used for testing.
	double testList[SIZE]{};  // <--- Never used. 


Hope that helps,

Andy
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std;

void readTestScores(double scoreList[], int size);
char getLetterGrade(double score);
void printComment(char grade);
void printTestResults(double scoreList[], int size);

int main()
{
    constexpr int size{10};
    double scoreList[size]{};
    
    readTestScores(scoreList, size);
    printTestResults(scoreList, size);
    
}

void readTestScores(double scoreList[], int size)
{
    for(size = 0; size < 10; size++)
    {
        cout << "Enter Test Score " << size+1 << ": ";
        cin >> scoreList[size];
    }
}

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 << "Very Good" << endl;
            break;
        case 'B':
            cout << "Good" << endl;
            break;
        case 'C':
            cout << "Satisfactory" << endl;
            break;
        case 'D':
            cout << "Need Improvement" << endl;
            break;
        case 'F':
            cout << "Poor" << endl;
            break;
    };
}

void printTestResults(double scoreList[], int size)
{
    cout << "Test Score\tLetter Grade\tComment" << endl;
    cout << "----------\t------------\t-------" << endl;
    
    for(size = 0; size < 10; size++)
    {
        cout << scoreList[size] << "\t\t\t\t" << getLetterGrade(scoreList[size]) << "\t\t\t";

        printComment(getLetterGrade(scoreList[size]));
    }
}


This runs fine in Xcode but I get these errors when compiling:

Program4.cpp: In function `int main()':
Program4.cpp:23: error: `constexpr' undeclared (first use this function)
Program4.cpp:23: error: (Each undeclared identifier is reported only once for each function it appears in.)
Program4.cpp:23: error: expected `;' before "int"
Program4.cpp:24: error: `size' undeclared (first use this function)
Program4.cpp:24: error: expected primary-expression before "double"
Program4.cpp:24: error: expected `;' before "double"
Program4.cpp:30: error: `scoreList' undeclared (first use this function)
It appears that you're compiling with a pre-C++11 compiler. You should really see if you can compile to one of the most current standards preferably C++14. if that's not possible replace constexpr with const.

That is my only option as of now. I get all the same errors after replacing constexpr with const.
Topic archived. No new replies allowed.