Crazy grades

I've been trying to make this work, the text file is called "grades", and this the numbers I'm given by the instructor in this file:
44 55 77 88
79 88 100 99
77 99 98 99
100 88 89 100
55 56 40 77
100 100 99 95
88 84 87 88
96 97 99 100
30 44 77 55
79 77 88 0
54 52 60 77
88 77 88 77
44 77 10 95
My code:
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    const int SIZE = 14;
    char letterGrade;
    const int NUMSCORES = 53;
    int studentGrades[NUMSCORES];
    int averages[SIZE];
    inFile.open("grades.txt");

        inFile >> studentGrades[1];
        inFile >> studentGrades[2];
        inFile >> studentGrades[3];
        inFile >> studentGrades[4];
        inFile >> studentGrades[5];
        inFile >> studentGrades[6];
        inFile >> studentGrades[7];
        inFile >> studentGrades[8];
        inFile >> studentGrades[9];
        inFile >> studentGrades[10];
        inFile >> studentGrades[11];
        inFile >> studentGrades[12];
        inFile >> studentGrades[13];
        inFile >> studentGrades[14];
        inFile >> studentGrades[15];
        inFile >> studentGrades[16];
        inFile >> studentGrades[17];
        inFile >> studentGrades[18];
        inFile >> studentGrades[19];
        inFile >> studentGrades[20];
        inFile >> studentGrades[21];
        inFile >> studentGrades[22];
        inFile >> studentGrades[23];
        inFile >> studentGrades[24];
        inFile >> studentGrades[25];
        inFile >> studentGrades[26];
        inFile >> studentGrades[27];
        inFile >> studentGrades[28];
        inFile >> studentGrades[29];
        inFile >> studentGrades[30];
        inFile >> studentGrades[31];
        inFile >> studentGrades[32];
        inFile >> studentGrades[33];
        inFile >> studentGrades[34];
        inFile >> studentGrades[35];
        inFile >> studentGrades[36];
        inFile >> studentGrades[37];
        inFile >> studentGrades[38];
        inFile >> studentGrades[39];
        inFile >> studentGrades[40];
        inFile >> studentGrades[41];
        inFile >> studentGrades[42];
        inFile >> studentGrades[43];
        inFile >> studentGrades[44];
        inFile >> studentGrades[45];
        inFile >> studentGrades[46];
        inFile >> studentGrades[47];
        inFile >> studentGrades[48];
        inFile >> studentGrades[49];
        inFile >> studentGrades[50];
        inFile >> studentGrades[51];
        inFile >> studentGrades[52];

    // I set up two arrays, one to hold the individual grades and the other to hold averages.
    //The second array (averages) is intended to take the numbers brought in from the .txt file provided and calculate averages;
        averages[1] = ((studentGrades[1] + studentGrades[2] + studentGrades[3] + studentGrades[4])/4);
        averages[2] = ((studentGrades[5] + studentGrades[6] + studentGrades[7] + studentGrades[8])/4);
        averages[3] = ((studentGrades[9] + studentGrades[10] + studentGrades[11] + studentGrades[12])/4);
        averages[4] = ((studentGrades[13] + studentGrades[14] + studentGrades[15] + studentGrades[16])/4);
        averages[5] = ((studentGrades[17] + studentGrades[18] + studentGrades[19] + studentGrades[20])/4);
        averages[6] = ((studentGrades[21] + studentGrades[22] + studentGrades[23] + studentGrades[24])/4);
        averages[7] = ((studentGrades[25] + studentGrades[26] + studentGrades[27] + studentGrades[28])/4);
        averages[8] = ((studentGrades[29] + studentGrades[30] + studentGrades[31] + studentGrades[32])/4);
        averages[9] = ((studentGrades[33] + studentGrades[34] + studentGrades[35] + studentGrades[36])/4);
        averages[10] = ((studentGrades[37] + studentGrades[38] + studentGrades[39] + studentGrades[40])/4);
        averages[11] = ((studentGrades[41] + studentGrades[42] + studentGrades[43] + studentGrades[44])/4);
        averages[12] = ((studentGrades[45] + studentGrades[46] + studentGrades[47] + studentGrades[48])/4);
        averages[13] = ((studentGrades[49] + studentGrades[50] + studentGrades[51] + studentGrades[52])/4);

    for (int a = 1; a < SIZE; a++)
        {
            cout << "Student #" << a << "'s grade is: " << averages[a];
               {
                if (averages[a] < 60)
                    letterGrade = 'F';
                else if (averages[a] >= 60 and averages[a] < 70)
                    letterGrade = 'D';
                else if (averages[a] >= 70 and averages[a] < 80)
                    letterGrade = 'C';
                else if (averages[a] >=80 and averages[a] < 90)
                    letterGrade = 'B';
                else
                    letterGrade = 'A';
                }
                    cout << " " << letterGrade << endl;

        }

    inFile.close();
    return 0;
}


and after typing this all out I get insanely high numbers like
"Student #1's grade is: 2890246 A"

I have NO idea where these numbers are coming from...
Help please T_T
Have you ever heard about loops? Looping through your file retrieving each student's grades.

Also you may want to consider using a multidimensional array so your student's grades are easier to extract and report.

And don't forget that arrays start at zero and end at size - 1.

i suspect that "Student #1's grade is: 2890246 A" is a result of garbage values contained in the arrays. A likely cause might be that the file is not opened properly.
Try inserting at line 14 (just after opening the file):
1
2
3
4
    if (!inFile.is_open())
    {
        cout << "Error: file is not open" << endl;
    }


Also, echoing the comment from jlb, it looks as though you only ever need to deal with 4 grades at a time, so just one array int studentGrades[4]; would suffice.
I thought about using one but I thought it would be easier to do it the way I did it- granted I wasn't expecting 100000+ answers as a result -.-'

and yeah I forgot that I have to start an array at 0 T_T'
sifting through my code trying to get the numbers all correct...


How do I fix the file not opening correctly? Does the file have to be in a certain location or what?
Last edited on
If you only specify a filename, it assumes that the file is in the same directory as the executable. (Note that this might not be true while running the program through some IDEs, if this is the case, then try copying the file to both where you have the source code, and to the directory with the executable, if you aren't sure which it's trying to get resources from)
Last edited on
After trying the 2D array along with a nested for loop, I GOT IT TO WORK!
Thank you all for the 100% I'm going to get xD

here's the code:
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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
        ifstream inFile;
    inFile.open("grades.txt");

    if (!inFile.is_open())
    {
        cout << "Error: file is not open" << endl;
    }
    const int NUM_STUDENTS = 13, NUM_GRADES = 4;
    double exams[NUM_STUDENTS][NUM_GRADES];
    double total;
    double average;
    char letterGrade;
    for (int i = 0; i < NUM_STUDENTS; i++)
    {
        for (int j = 0; j < NUM_GRADES; j++)
        {
            inFile >> exams[i][j];
        }
    }
    for (int rows = 0; rows < NUM_STUDENTS; rows++)
    {
        total = 0;
        for (int col = 0; col < NUM_GRADES; col++)
            total += exams[rows][col];
            average = total/NUM_GRADES;
        cout << "Score for Student #" << (rows + 1) << " is " << average;
                {
                if (average < 60)
                    letterGrade = 'F';
                else if (average >= 60 and average < 70)
                    letterGrade = 'D';
                else if (average >= 70 and average < 80)
                    letterGrade = 'C';
                else if (average >= 80 and average < 90)
                    letterGrade = 'B';
                else
                    letterGrade = 'A';
                }
        cout << " " << letterGrade << endl;
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.