How to output data from text file? (Student Report Card Program)

I'm currently writing a program that consists of displaying students' ID, Name, Course, Credit, and Score. The data is all in the text file.

"StudentRecords.txt"
1
2
3
4
5
6
7
8
9
10
11
12
12546 Amy   CS1 4 81 
13455 Bill  CS1 4 76
14328 Jim   CS1 4 64
14388 Henry CS3 3 80
15667 Peter CS3 3 45
12546 Amy   CS2 4 90 
13455 Bill  CS2 4 85
14328 Jim   CS2 4 71

12546 Amy   CS3 3 90 
13455 Bill  CS3 3 75
14328 Jim   CS3 3 69


The following table was used to calculate the GPA(just a reference):
1
2
3
4
5
6
Range Grade:
90 -- 100 > 4.0
80 -- 89 > 3.0
70 -- 79 > 2.0
60 -- 69 > 1.0
0 -- 59 > 0.0


The problem I'm having right now is my output. I'm trying to get it to match my expected output, but I can't seem to figure it out. It probably has to do with some missing else if() statements in the second for loop.

I'm still new to programming, but if I could get any advice on how to get my output to match the expected output, I would appreciate it!


My Current 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
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

struct Student
{
    int ID = -1;
    string Name = "";
    string Course = "";
    int Credit = -1;
    int Score = -1;
};

const int SIZE = 99;

int main()
{
    ifstream inputFile;
    string fileName = "StudentRecords.txt";
    Student studArr[SIZE];

    inputFile.open(fileName.c_str(), ios::in);

    int n = 0;

        if (inputFile.is_open())
        {
            while(!inputFile.eof())
            {
                Student st;
                inputFile >> st.ID;
                inputFile >> st.Name;
                inputFile >> st.Course;
                inputFile >> st.Credit;
                inputFile >> st.Score;
                studArr[n] = st;
                n++;
            }

            inputFile.close();
        }
        else
        {
            cout << "File cannot be opened.";
            return 1;
        }

        // sorts the array by ID and Course
        for (int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                if(studArr[i].ID > studArr[j].ID)
                {
                    Student temp = studArr[i];
                    studArr[i] = studArr[j];
                    studArr[j] = temp;
                }
            }
        }
        int check = 0;
        float dividend = 0;
        float divisor = 0;
        for(int i = 0; i < n; i++)
        {
            if(studArr[i].ID != studArr[check].ID)
            {
                cout << "======================\nGPA " << round((dividend / divisor)) << endl << endl;
            }
            else if()
            {

            }
            else if()
            {
               
            }
            else if(i == 0)
            {
                cout << studArr[i].ID << " " << studArr[i].Name << endl << endl;
                dividend = 0;
                divisor = 0;
            }

            float gpa;
            if(studArr[i].Score < 60)
            {
                gpa = 0.0;
            }
            else if(studArr[i].Score < 70)
            {
                gpa = 1.0;
            }
            else if(studArr[i].Score < 80)
            {
                gpa = 2.0;
            }
            else if(studArr[i].Score < 90)
            {
                gpa = 3.0;
            }
            else if(studArr[i].Score < 100)
            {
                gpa = 4.0;
            }

            dividend += gpa * studArr[i].Credit;
            divisor += studArr[i].Credit;
            cout << studArr[i].Course << " " << studArr[i].Score << " "
                 << setprecision(1) << fixed << gpa << ' ' << endl << endl;
        }
        cout << "======================\nGPA " << round((dividend / divisor)) << endl << endl;
        cout << endl << endl;
        return 0;
}


My Current Output:
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
12546 Amy

CS1 81 3.0

CS2 90 4.0

CS3 90 4.0

======================
GPA 4.0

CS2 85 3.0

======================
GPA 3.0

CS1 76 2.0

======================
GPA 3.0

CS3 75 2.0

======================
GPA 3.0

CS1 64 1.0

======================
GPA 3.0

CS2 71 2.0

======================
GPA 3.0

CS3 69 1.0

======================
GPA 2.0

CS3 80 3.0

======================
GPA 3.0

CS3 45 0.0

======================
GPA 2.0


Expected Output:
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
12546 Amy

CS1 4 81 3.0

CS2 4 90 4.0

CS3 3 90 4.0

======================

GPA 3.64

======================
13455 Bill

CS1 4 76 2.0

CS2 4 85 3.0

CS3 3 75 2.0

======================

GPA 2.36

======================
14328 Jim

CS1 4 64 1.0

CS2 4 71 2.0

CS3 3 69 1.0

======================

GPA 1.36

======================

14388 Henry

CS3 3 80 3.0

======================

GPA 3

======================
15667 Peter

CS3 3 45 0.0

======================
 
GPA 0
Last edited on
Seems like overall GPA is the GPA of all semesters added up and then divided by the amount of semester that were added together.


As for after that, I don't know what you're doing. Just output their ID, space, their name, 2 new lines, their classes, credits, grade, GPA, and then "=====", and their overall GPA under that. All the data you need is in the student array.

Try getting the output correct just for the first person. Once you have, then you can use a loop to have it output the same way for each person after that.
your output may be stale. The version posted will not compile, so whatever you ran is the last version before you made changes to it that did compile, which may be wrong (probably is, since you were editing it).

once you get it in sync,
the output loop looks too complex.
the variable check is never changed in that loop, so it only works as you think it should on the first one (where check == 0). At least it looks wrong to me with that logic.

nevermind, take a look at these 2 things and ask again after that... thought I saw something else.
Last edited on
Topic archived. No new replies allowed.