Reading .txt file HELP

I am given a .txt file and it contains two columns of multiple numbers separated by some spaces. The first column is an identification number while the following number in that row is a GPA. I need to find the average GPA for each identification number. An example is
1A 4.00
1A 3.79
1B 3.9
2A 4.0
2A 3.9

I need to categorize the averages by 1A, 1B, 2A, etc. I know how to input the text file but I am lost from there. I then need to categorize the GPA average from a certain GPA and up, such as all GPAs above a 3.5. Please help!
Last edited on
This is a rough draft of the code resembling your request. You can modify it to your liking.

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <vector>
typedef unsigned short ushort;
using namespace std;

class Grades
{
    char average[3];
    float gpa;
    bool is1A, is1B, is2A;

    void assignBools();
public:
    Grades(const string&);

    string getAverage() const {return average;}
    float getGPA() const {return gpa;}
    bool getIs1A() const {return is1A;}
    bool getIs1B() const {return is1B;}
    bool getIs2A() const {return is2A;}
};

void mainMenu(vector <Grades>);

int main()
{
    ifstream iFile("grades.txt");
    vector <Grades> data;
    string line;

    if(iFile)
    {
        while(getline(iFile, line))
            data.push_back(Grades(line));

        iFile.close();
        mainMenu(data);
    }
    else
        cout << "Could not open file!\n";

    return 0;
}

Grades::Grades(const string& line)
{
    const ushort LINE_LENGTH = line.length();

    for(ushort i = 0; i < LINE_LENGTH; i++)
    {
        if(line[i] == ' ')
        {
            string floatHold;

            for(ushort j = i + 1; j < LINE_LENGTH; j++)
                floatHold += line[j];

            average[2] = '\0';
            gpa = atof(floatHold.c_str());
            break;
        }

        average[i] = line[i];
    }

    assignBools();
}
void Grades::assignBools()
{
    if(average[0] == '1' && average[1] == 'A')
    {
        is1A = true;
        is1B = false;
        is2A = false;
    }
    else if(average[0] == '1' && average[1] == 'B')
    {
        is1A = false;
        is1B = true;
        is2A = false;
    }
    else
    {
        is1A = false;
        is1B = false;
        is2A = true;
    }
}
void mainMenu(vector <Grades> data)
{
    const ushort VECTOR_SIZE = data.size();
    char main_menuAnswer;

    do
    {
        cout << "\t\t\tMAIN MENU v1.0\n\n-----\n1 - Search\n2 - Exit\n-----\nAnswer: ";
        cin >> main_menuAnswer;

        system("CLS");

        switch(main_menuAnswer)
        {
        case '1':
            {
                char search_menuAnswer;

                cout << "\t\t\tSEARCH MENU v1.0\n\nSearch by:\n-----\n1 - Average (e.g. 1A)\n2 - GPA\n-----\nAnswer: ";
                cin >> search_menuAnswer;

                system("CLS");

                switch(search_menuAnswer)
                {
                case '1':
                    {
                        string typedAverage;

                        cout << "\t\t\tAVERAGE SEARCH v1.0\n\nType Average to search: ";
                        cin >> typedAverage;
                        cout << "-----\n";

                        for(ushort i = 0, counter = 0; i < VECTOR_SIZE; i++)
                        {
                            if(typedAverage == data[i].getAverage())
                            {
                                counter++;
                                cout << data[i].getAverage() << ' ' << data[i].getGPA() << endl;
                            }

                            if(i == VECTOR_SIZE - 1 && counter == 0)
                                cout << "No data found for average: " << typedAverage << endl;
                        }

                        cout << "-----\n\n";
                        system("PAUSE");
                        system("CLS");
                        break;
                    }
                case '2':
                    {
                        float typedGPA;
                        char gpa_menuAnswer;

                        cout << "\t\t\tGPA SEARCH v1.0\n\nType GPA: ";
                        cin >> typedGPA;
                        cout << "-----\n1 - <\n2 - >\n3 - =\n-----\nAnswer: ";
                        cin >> gpa_menuAnswer;
                        cout << "\n-----\n";

                        for(ushort i = 0, counter = 0; i < VECTOR_SIZE; i++)
                        {
                            if(gpa_menuAnswer == '1')
                            {
                                if(typedGPA < data[i].getGPA())
                                {
                                    counter++;
                                    cout << data[i].getAverage() << ' ' << data[i].getGPA() << endl;
                                }
                            }
                            else if(gpa_menuAnswer == '1')
                            {
                                if(typedGPA > data[i].getGPA())
                                {
                                    counter++;
                                    cout << data[i].getAverage() << ' ' << data[i].getGPA() << endl;
                                }
                            }
                            else
                                if(typedGPA == data[i].getGPA())
                                {
                                    counter++;
                                    cout << data[i].getAverage() << ' ' << data[i].getGPA() << endl;
                                }

                            if(i == VECTOR_SIZE - 1 && counter == 0)
                                cout << "No data found for GPA: " << typedGPA << endl;
                        }

                        cout << "-----\n\n";
                        system("PAUSE");
                        system("CLS");
                    }
                }
            }
        }
    }while(main_menuAnswer != '2');
}
To compute the average GPA, you'll need to keep track of the sum of the GPAs and the number of them.
1
2
3
4
5
6
// statistics
struct Stats {
    double sum;
    unsigned count;
    Stats() : sum(0), count(0) {;}
};

You need a way to look up one of these for each ID. A map is perfect for 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
#include <iostream>
#include <map>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::map;
using std::string;

// statistics
struct Stats {
    double sum;
    unsigned count;
    Stats() : sum(0), count(0) {;}
};


typedef map<string,Stats> MyMap; // maps ID to statistics

int
main()
{
    MyMap theMap;
    string id;
    double gpa;

    while (cin >> id >> gpa) {
        Stats &s = theMap[id];  // find the Stats struct for the ID
        s.sum += gpa;           // adjust the gpa and count
        ++s.count;
    }

    cout << "ID\tAverage\n";
    for (auto &x : theMap) {
        Stats &s(x.second);
        cout << x.first << '\t' << s.sum / s.count << '\n';
    }

    return 0;
}

Topic archived. No new replies allowed.