structs array sorting

i have a question about sorting the array member function with arrays and structs
im trying to make a function that it going to order the apply dates which is the first column on dates
i need to make a member i havent did it yet just testing to using swap and ordermonths if i can get the date without it any advice please not sure what to do


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
190
191
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <cstdlib>
using namespace std;
struct Date
{
    int month;
    int day;
    int year;
    Date();
};
struct Student
{
    int ID;
    string name;
    char letterGrade;
    string gpa;
     Date apply;
    Date grad;
    Student();

};
Date::Date()
{
    month =0;
    day =0;
    year=0;
}
Student::Student()
{
    ID =0;
    name ="";
    gpa ="";
    letterGrade =' ';

}
Student* readStudentFile(int &totalStudent);
void menu(Student *Student, int totalStudent);
void printStudent(Student *Student, int totalStudent , char flag);
void swapNum(int *num, int *num2);
void orderMonths(Student *Student, int totalStudent);

int main()
{
    Student *allStudent;
    int totalStudent =0;
    allStudent = readStudentFile(totalStudent);
    if(allStudent == NULL)
    {
        cout << "student is null " << endl;
        return 0;
    }
    orderMonths(allStudent,totalStudent);
    printStudent(allStudent,totalStudent,' ');
    menu(allStudent,totalStudent);

    return 0;
}
Student* readStudentFile(int &totalStudent)
{
    char delimter;
    Student *studentPointer;
    ifstream studentFile;

    studentFile.open("students.txt");

    if(!studentFile)
    {
        cout << "error" << endl;
        return NULL;
    }
    cout << "success" << endl;

    studentFile >> totalStudent;
    studentPointer = new Student[totalStudent];

    cout << "total students: " << totalStudent << endl;

    for(int i = 0; i < totalStudent; i++)
    {
        studentFile >>studentPointer[i].ID;
        studentFile >> studentPointer[i].name;
        studentFile >> studentPointer[i].letterGrade;
        studentFile >> studentPointer[i].gpa;

        studentFile >> studentPointer[i].apply.month;
        studentFile >> delimter;
        studentFile >> studentPointer[i].apply.day;
        studentFile >> delimter;
        studentFile >> studentPointer[i].apply.year;

        studentFile >> studentPointer[i].grad.month;
        studentFile >> delimter;
        studentFile >> studentPointer[i].grad.day;
        studentFile >> delimter;
        studentFile >> studentPointer[i].grad.year;
    }
    studentFile.close();
    return studentPointer;

}
void menu(Student *Student, int totalStudent)
{
    int input;
    while(true)
    {
        cin >> input;
        switch(input)
        {
        case 0:
            printStudent(Student,totalStudent,' ');
            break;
        case 1:
            printStudent(Student,totalStudent,'p');
            break;
        case 2:
            printStudent(Student,totalStudent,'f');
        case 3:
            orderMonths(Student, totalStudent);
            break;
        case 4:
            exit(0);
        }
    }

}
void printStudent(Student *Student, int totalStudent , char flag)
{
    if(Student == NULL || totalStudent < 1)
    {
        return;
    }
    cout << endl;

    for(int i =0; i < totalStudent; i++)
    {
        if(flag != ' ')
        {
            if((flag == 'p' && Student[i].gpa != "pass" ) || (flag == 'f' && Student[i].gpa != "fail" ) )
           {
            continue;
           }
        }
        cout.clear();
        cout.fill(' ');

        cout
        << left
        << setw(10)
        << i
        << left << setw(10)
        <<" "<<Student[i].name
        << left << setw(3)
        <<" \t"<<Student[i].letterGrade

        << left << setw(10)
        <<" \t"<<Student[i].gpa
        << left
        <<"\t"<<Student[i].apply.month << ":" << Student[i].apply.day << ":" << Student[i].apply.year
        << left << "\t"
        << Student[i].grad.month << ":" << Student[i].grad.day << ":" << Student[i].grad.year
        << "\t";
         cout << endl;
    }

    cout << endl;
    cout << "press 0 for all students" <<endl;
    cout << "press 1 for pass " << endl;
    cout << "press 2 for fail " << endl;
    cout << "press 3 sorted apply dates" << endl;
    cout << "press 4 to exit " << endl;


}
void swapNum(int *num, int *num2)
{
    int temp = *num;
    *num = *num2;
    *num2 =temp;
}
void orderMonths(Student *Student, int totalStudent)
{
     for(int i=0; i< totalStudent-1; ++i)
         for(int j=i+1; j <totalStudent; j++)
             if(Student[i].apply.day > Student[j].apply.day)
                 swapNum(Student[i].apply.day , *Student[j].apply.day);


}



6
0  Matt A  pass 5:6:2015 6:5:2016
1  Mike A  pass 5:8:2015 6:10:2016
2  Don  B  pass 5:2:2015 6:11:2016
3  Jake C  fail 5:6:2015 6:15:2016
4  Kim  D  fail 5:3:2015 6:16:2016
5  Tom  C  fail 5:4:2015 6:17:2016

Last edited on
Overload operator < for Date type and use that to further overload operator < for Student type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Date
{
    int month;
    int day;
    int year;
    Date();
};
struct Student{Date apply;};
bool operator < (const Date& lhs, const Date& rhs)
{
    return ((lhs.year < rhs.year) || ((lhs.year == rhs.year) && (lhs.month < rhs.month)) ||
        ((lhs.year == rhs.year) && (lhs.month == rhs.month) && (lhs.day < rhs.day)));
}
bool operator < (const Student& lhs, const Student& rhs)
{
    return lhs.apply < rhs.apply;
}

Then you can sort the array of Student objects just as you'd an array of POD types. And if you were to use std::vector<Student> then you could std::sort() the vector with a custom comparator either using a function object or lambda
is there anyway to do it without using an operator
and im not sure what your doing with student
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
// void orderMonths(Student *Student, int totalStudent)
void order_by_apply_date( Student *Student, int totalStudent )
{
     for(int i=0; i< totalStudent-1; ++i)
     {
         for(int j=i+1; j <totalStudent; j++)
         {
             // convert the dates into a single number yyyymmdd
             const int a = Student[i].apply.year * 10000 +
                           Student[i].apply.month * 100 +
                           Student[i].apply.day ;

             const int b = Student[j].apply.year * 10000 +
                           Student[j].apply.month * 100 +
                           Student[j].apply.day ;

             if( a > b ) // compare these two numbers; if out of order
             {
                 // swap the two students by using a temporary object
                 // note: ideally, avoid hiding the name of the type Student
                 struct Student temp = Student[i] ;
                 Student[i] = Student[j] ;
                 Student[j] = temp ;
             }

         }
     }
}
why do we use struct their at line 21
i kept getting an error with temp is say temp was not declared
im trying to order the apply dates to the id so for example
when the user enter 3 the data should be shown like this:


0 Don  B  pass 5:2:2015 6:11:2016
1 Kim  D  fail 5:3:2015 6:16:2016
2 Tom  C  fail 5:4:2015 6:17:2016
3 Jake C  fail 5:6:2015 6:15:2016
4 Matt A  pass 5:6:2015 6:5:2016
5 Mike A  pass 5:8:2015 6:10:2016

> why do we use struct their at line 21

struct Student is an elaborated type specifier.
http://en.cppreference.com/w/cpp/language/elaborated_type_specifier

Required because the type 'Student' is hidden by the parameter 'Student' (non-type)
void order_by_apply_date( Student *Student, int totalStudent )

You should not get an error with the elaborated type specifier.
If you do, the compiler is broken; try this instead:

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
// void orderMonths(Student *Student, int totalStudent)
void order_by_apply_date( Student *Student_Array, int totalStudent ) // avoid hiding the type
{
     for(int i=0; i< totalStudent-1; ++i)
     {
         for(int j=i+1; j <totalStudent; j++)
         {
             // convert the dates into a single number yyyymmdd
             const int a = Student_Array[i].apply.year * 10000 +
                           Student_Array[i].apply.month * 100 +
                           Student_Array[i].apply.day ;

             const int b = Student_Array[j].apply.year * 10000 +
                           Student_Array[j].apply.month * 100 +
                           Student_Array[j].apply.day ;

             if( a > b ) // compare these two numbers; if out of order
             {
                 // swap the two students by using a temporary object
                 // note: ideally, avoid hiding the name of the type Student
                 Student temp = Student_Array[i] ;
                 Student_Array[i] = Student_Array[j] ;
                 Student_Array[j] = temp ;
             }

         }
     }
}

that didnt work could i do it in the printstudent function
i got it nevermind i figure it out
Topic archived. No new replies allowed.