Outputting Sorted Data and Reset GPA

I'm currently writing a program where it'll display the student records in 4 columns(ID, first name, last name, and GPA), sort their records in ascending order, and reset each of their GPA to 0.0 from the sorted student records.

The student records are stored in a text file I called "StudentInfo.txt"

1
2
3
4
5
6
123456789   John    Johnson    3.5
512434990   Mary    Jackson    3.9
342432444   Peter   Young      2.3
470068625   Jim     Lee        2.9
234324324   Tammy   Gaddis     3.1
121219000   Ester   Schwab     2.7


I've done most of the code, but the only thing I'm having trouble with is outputting the sorted student records and the reset GPA. If anyone can help me out on this, 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
#include <string>
#include <iomanip>
#include <fstream>
#include <iostream>

using namespace std;

struct studentInfo
{
    int ID;
    string firstName;
    string lastName;
    double GPA;
};

const int SIZE = 100;
void display(studentInfo *arr[]);
void resetGPA(studentInfo *arr[], int);
void sortStud(studentInfo *arr[], int);

int main()
{
    int counter = 0;
    int ID;
    string firstName;
    string lastName;
    double GPA;
    studentInfo *arrStud[SIZE];

    ifstream inputFile;

    inputFile.open("StudentInfo.txt");
    if(inputFile.is_open())
    {
        cout << "ID:" << setw(15) << "Name:" << setw(14) << "GPA:" << endl;
        cout << "-------------------------------------------" << endl;

        while(!inputFile.eof())
        {
            inputFile >> ID >> firstName >> lastName >> GPA;
            studentInfo tmp;

            tmp.ID = ID;
            tmp.firstName = firstName;
            tmp.lastName = lastName;
            tmp.GPA = GPA;

            arrStud[counter] = &tmp;

            cout << arrStud[counter]->ID
                 << setw(8) << arrStud[counter]->firstName
                 << setw(10) << arrStud[counter]->lastName
                 << setw(8) << arrStud[counter]->GPA
                 << endl;

            counter++;
        }
        cout << "-------------------------------------------" << endl;

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


    cout << endl;
    cout << "Sorting Students by ID..." << endl;
    cout << endl;
    sortStud(arrStud, counter);
    display(arrStud);
    cout << endl;
    cout << "Resetting GPA Data..." << endl;
    cout << endl;
    resetGPA(arrStud, counter);
    display(arrStud);
}

void display(studentInfo *arr[])
{

}

void resetGPA(studentInfo *arr[], int records)
{
    studentInfo tmp;

    for (int i = 0; i < records; i++)
    {
        arr[i]->GPA = 0.0;
    }
}

void sortStud(studentInfo *arr[], int records)
{
    studentInfo tmp;

    for (int i = 0; i < records; i++)
        for (int j = 0; j < records - i - 1; j++)
            if (arr[j]->ID > arr[j + 1]->ID)
            {
                arr[records + 1] = new studentInfo;
                arr[records + 1] = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = arr[records + 1];
            }

            arr[records + 1] = nullptr;
}



My Current Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ID:          Name:          GPA:
___________________________________
123456789    John   Johnson     3.5
512434990    Mary   Jackson     3.9
342432444   Peter     Young     2.3
470068625     Jim       Lee     2.9
234324324   Tammy    Gaddis     3.1
121219000   Ester    Schwab     2.7
___________________________________


Sorting Students by ID...


Resetting GPA Data...




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
ID:          Name:          GPA:
___________________________________
123456789    John   Johnson     3.5
512434990    Mary   Jackson     3.9
342432444   Peter     Young     2.3
470068625     Jim       Lee     2.9
234324324   Tammy    Gaddis     3.1
121219000   Ester    Schwab     2.7
___________________________________


Sorting Students by ID...

ID:          Name:          GPA:
___________________________________
512434990    Mary   Jackson     3.9
123456789    John   Johnson     3.5
234324324   Tammy    Gaddis     3.1
470068625     Jim       Lee     2.9
121219000   Ester    Schwab     2.7
342432444   Peter     Young     2.3
___________________________________


Resetting GPA Data...

ID:          Name:          GPA:
___________________________________
512434990    Mary   Jackson     0.0
123456789    John   Johnson     0.0
234324324   Tammy    Gaddis     0.0
470068625     Jim       Lee     0.0
121219000   Ester    Schwab     0.0
342432444   Peter     Young     0.0
___________________________________
Last edited on
Your code shouldn't even compile.

But do you realize that all your input is local to your display() function?

@Orion98, why are you using pointers?

What you are pointing to won't live longer than the while() loop it is defined in. Just use straightfoward structs.

Don't use .eof() to test for no more records. This won't turn true until after you've tried to read the data ... by which time it will be too late and you'll end up with (here) duplicate records.

As @jlb says, your input is obviously in the wrong place.
Last edited on
@jlb and @lastchance; I replaced my code here that actually compiles and reproduces the problem, which is outputting the sorted student records and their reset GPA.
@Orion98
- When you change your code please put any revised version BELOW, or the thread will cease to make sense.
- Please make the changes we suggested and then we can look at it again.

As far as input is concerned, your display() function currently (i.e. at the point where I'm writing this) does nothing, so you aren't going to be outputting sorted data, etc., and you are still outputting in the same loop as your input.

And get rid of those dratted pointers!
@lastchance; it's actually part of my assignment to use pointers and its working just fine for my first part (which is outputting the students records in 4 columns by ID, first name, last name, and GPA). If I delete my pointers, my code would not work the way I want it to.
I still recommend doing it first without pointers and then, if really necessary, modifying it to accommodate them.

We don't know what your assignment entails - many array operations can be done with a pointer-like syntax and that may be what was intended. That is different from what you are currently doing (having an array of pointers).

If you point to the memory address of a variable with a finite lifetime (e.g. the loop local variable studentInfo tmp;), then at the end of its lifetime (for tmp, the end of the loop) that variable, and hence the address you are pointing to, will cease to exist. The fact that it is working "fine" is simply that you output each student as soon as you read it and before the end of the loop.
Last edited on
Registered users can post here. Sign in or register to post.