Alphabetical Sort Using Overload???

I have to write a program in OOP format that reads in student names/grades from a user provided file, outputs the raw data, sorts & outputs the data by alphabetical order, and sorts/outputs the data by final average.

My professor provided me with the skeleton of the program, posted below. I have no idea how to sort the student data alphabetically. In the assignment instructions, it said that the class contains overloads and that the < (less than) operator should be used to compare student names.

Can one of you please show me some sample code of how this is supposed to work? I have no clue whatsoever how to do the alphabet sort. So far, I have a general idea of how to knock out the other parts of the program.

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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

#include "Student.h"

#define ARRAYSIZE 12


/*****************************************************************************/
/* Function Prototypes                                                       */
/* Description: Informs compiler about functions to be used in program.      */
/*****************************************************************************/

// read the data file, store in arrays
int readDatafile(Student[], int);
// output the formatted "raw data"
void dumpData(Student[]);
// output the names, average, grade data (formatted)
void dumpSorted(Student[]);

// sorting routines - use a bubble sort
void sortAscend(Student[]);  // set this up to sort by name
void sortDescend(Student[]); // set this up to sort by average
void swap( ... );        // used by the bubble sort


/*****************************************************************************/
/* Function name: MAIN                                                       */
/* Description: Triggers all of the functions in the order listed            */
/*****************************************************************************/

main()
{
  Student slist[ARRAYSIZE];
  
  int readDatafile();
  dumpData();
  sortAscend();
  sortDescend();
  dumpSorted();

  return 0;
}


/*****************************************************************************/
/* Function name: readDatafile                                               */
/* Description: Opens & reads the datafile, then returns # of entries read   */
/*****************************************************************************/

int readDatafile(Student list[], int max)
{
  string fname;
  ifstream ifs;
  int num=0;

  cout << "Enter file Name: ";
  cin >> fname;
  ifs.open(fname.c_str());
  if(ifs.fail()) 
  {
    cout << "Error opening file: " << fname << endl;
    return 0;
  }

  // assumes each data line is well formatted
  while(num < max && ifs >> list[num])
  {
    num++;
  }

  ifs.close();
  return num;
}


/*****************************************************************************/
/* Function name: dumpData                                                   */
/* Description: Output the data that was read in                             */
/*****************************************************************************/

void dumpData(Student list[])
{
    cout << endl;
    cout << "Raw Grades:"<< endl;
    cout << "----------" << endl;
    for(int i = 0; i < max; i ++)
  {
	cout << setw(2) << list[i].getName() << " / "
    cout << setw(2) << list[i].getTests() << " / ";
    cout << setw(2) << list[i].getProgs() << " / ";
    cout << list[i].getFinal();
    cout << endl;
  }
}


/*****************************************************************************/
/* Function name: sortAscend                                                 */
/* Description: Sort student data in alphabetical order                      */
/*****************************************************************************/

void sortAscend(Student list[])
{

}


/*****************************************************************************/
/* Function name: sortDescend                                                */
/* Description: Sort student data by average from highest to lowest          */
/*****************************************************************************/

void sortDescend(Student list[])
{
  // flag to determine if a swap has occurred
  // flag is initialized to true to start first pass
  bool flag= true;
  for(int i = 1; (i <= length) && flag; i++)
  {
    flag = false;

    for(int j=0; j < (length -1); j++)
	{

      if (list[j+1] > list[j])
	  {
        // swap elements
        swap(list[j], list[j+1]);
        flag = true; // indicates that a swap occurred.
      }
    }
  }
}


/*****************************************************************************/
/* Function name: dumpSorted                                                 */
/* Description: Output the final sorted information                          */
/*****************************************************************************/

void dumpSorted(Student list[])
{
  cout << endl;
  cout << "Grades:" << endl;
  cout << "------" << endl;
  for(int i=0; i<max; i++)
  {
	cout << list[i] << endl;;
  }
}

void swap()
{
	
}


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
#ifndef STUDENT_H
#define STUDENT_H

#include <string>
using namespace std;

// struct definition for a student object
class Student {
 public:

  // constructor
  Student(string n="", int t=0, int p=0, int f=0);

  // modifiers
  void setName(string n);
  void setTests(int v);
  void setProgs(int v);
  void setFinal(int v);

  // inspectors
  string getName() const;
  int getTests() const;
  int getProgs() const;
  int getFinal() const;

  float getAverage() const;
  char getGrade() const;

  // overloads
  // greater than - compares grade "average"
  bool operator>(const Student &) const; // greater than
  // less than - compares student names
  bool operator<(const Student &) const; // less than

 private:
  string name;   // student name
  int tests;     // student test score
  int progs;     // student progam score
  int finalexam;     // student final exam score

  static const float testPCT;  // fraction of average for tests
  static const float progsPCT; // fraction of average for progs
  static const float finalPCT; // fraction of average for final
};

// overload of external stream operators
ostream &operator<<(ostream &, const Student &);
istream &operator>>(istream &, Student &);

#endif 


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
#include "Student.h"
#include <iostream>
#include <iomanip>
using namespace std;

/*****************************************************************************/
/* CONSTRUCTOR                                                               */
/* Default Constructor                                                       */
/*****************************************************************************/

Student::Student()
{
	
}

/*****************************************************************************/
/* MODIFIERS                                                                 */
/* Description:             */
/*****************************************************************************/

void Student::setName(string n)
{
	name = n;
}


void Student::setTests(int t)
{
	tests = t;
}


void Student::setProgs(int p)
{
	progs = p;
}


void Student::setFinal(int f)
{
	finalexam = f;
}


/*****************************************************************************/
/* INSPECTORS / GETTERS                                                      */
/* Description:             */
/*****************************************************************************/

string getName() const
{
	return name;
}


int getTests() const
{
	return tests;
}


int getProgs() const
{
	return progs;
}


int getFinal() const
{
	return finalexam;
}


float getAverage() const
{
	
}


char getGrade() const
{
	
}


/*****************************************************************************/
/* OVERLOADS                                                                 */
/* Description:             */
/*****************************************************************************/

// Greater than operator > will compare students grade averages
// Less than operator < will compare students names (string compare)

ostream &operator<<(ostream & output, const Student & G);
{
	//output << G.getAverage() << endl;
}

istream &operator>>(istream & input, Student & G);
{
	
}
Last edited on
I’m sure you have already solved your exercise, anyway:

Your code:
1
2
3
4
  // greater than - compares grade "average"
  bool operator>(const Student &) const; // greater than
  // less than - compares student names
  bool operator<(const Student &) const; // less than 


Possible solution:
1
2
3
4
5
6
7
8
9
bool Student::operator>(const Student& otherStudent) const
{
    return tests > otherStudent.getTests();
}

bool Student::operator<(const Student& otherStudent) const
{
    return name < otherStudent.getName();
}


As a test, in (a simplified version of) main you can try something like:
1
2
3
4
5
6
7
8
9
10
Student John, Bill;
John.setName("John");
John.setTests(5);
Bill.setName("Bill");
Bill.setTests(4);

if(Bill < John)
    cout << "Of course Bill comes first!" << endl;
if(John > Bill)
    cout << "But John is cleverer!" << endl;


Topic archived. No new replies allowed.