I'm having problems sorting an array of structs read from a file. I have a menu to sort by student number, student gpa, student name, and to find the average gpa's. The sort by gpa and student number are working fine, I thought it would be the same idea for sorting but it won't work. Any ideas how I can get the sort by name to work properly?
Code:
int main()
{
student s[MAXCLASSSZ]; //creats struct s[0], s[1]...s[MAXCLASSSZ -1]
int n; //counter for records
char choice; //user's choice from menu
double gpa_average; //local variable to hold calculated gpa average
decimal_format(cout);
//formates decimals nicely on the screen
explain_program(cout);
//tell user what program does
ifstream fin;
ofstream fout; //internal name for output file
//internal name for input file
fin.open("stu.in");
if(fin.fail())
{
cout << "\nInput file opening failed!";
exit(1); //terminate program execution
}
fout.open("stu.out");
if(fout.fail())
{
cout << "\nOutput file opening failed!";
exit (1); //terminate program execution
}
decimal_format(fout);
//formats decimals nicely in output file
//document file output
fout << "\nChanda Kolibas KMSV0606"
<< "\nAssignment #10 Question #1\n";
explain_program(fout);
//write to file what program does
read_array_of_struct(fin, s, n);
//reads student file information
print_menu(cout);
//prints menu choices
choice = get_choice();
while(choice != QUIT)
{
switch(toupper(choice))
{
case '0':
print_menu(cout);
break;
case '1':
sort_on_name(s, n);
cout << "\nSorted by name: \n\n";
fout << "\nSorted by name: \n\n";
write_array_of_struct(cout, s, n);
write_array_of_struct(fout, s, n);
break;
case '2':
sort_on_student_number(s, n);
cout << "\nSorted by student number: \n\n";
fout << "\nSorted by student number: \n\n";
write_array_of_struct(cout, s, n);
write_array_of_struct(fout, s, n);
break;
case '3':
sort_on_gpa(s, n);
cout << "\nSorted by gpa: \n\n";
fout << "\nSorted by gpa: \n\n";
write_array_of_struct(cout, s, n);
write_array_of_struct(fout, s, n);
break;
case '4':
gpa_average = average_cal(s, n);
cout << "\nThe average of all the GPA's is " << gpa_average << ".";
fout << "\nThe average of all the GPA's is " << gpa_average << ".";
break;
default:
cout << "Sorry, " << choice << " is invalid."
<< "\nPlease try again.";
}
choice = get_choice();
}
fin.close();
fout.close();
return 0;
}
void explain_program(ostream& os)
{
os << "This program gives the user a menu of options.\n";
}
void decimal_format(ostream& os)
{
os.setf(ios::fixed);
os.setf(ios::showpoint);
os.precision(2);
}
void read_array_of_struct(istream& is, student s[], int& n)
{
char temp[MAXNAMESZ], //temp storage for name
ch; //char used to read <eoln>
n = 0; //no records have been read yet
is.get(temp, MAXNAMESZ);
while((!is.eof()) && ( n < MAXNAMESZ))
{
strcpy_s(s[n].name, temp);
is >> s[n].student_number;
is >> s[n].gpa;
is.get(ch);
n++;
is.get (temp, MAXNAMESZ);
}
}
void write_array_of_struct(ostream& os, student s[], int& n)
{
for(int i = 0; i < n; i++)
{
os << s[i].name;
os.width(2);
os << s[i].student_number;
os.width(10);
os << s[i].gpa << endl;
}
}
void print_menu(ostream& os)
{
os << "\n0 - See Menu Again";
os << "\n1 - Sort by Name";
os << "\n2 - Sort by Student Number";
os << "\n3 - Sort by GPA";
os << "\n4 - Find Average GPA";
os << "\nQ - Quit this program\n\n\n";
}
char get_choice()
{
char answer; //local variable for choice storage
cout << "\nPlease enter your choice (0 to see menu again, Q to exit program): ";
cin >> answer;
return answer;
}
void sort_on_name(student s[], int n)
{
student temp;
int pass;
for(pass = 1; pass <= n-1; pass++);
{
for(int i = 0; i < n-pass; i++)
{
if(strcmp(s[i].name, s[i+1].name)>0)
{
temp = s[i];
s[i] = s[i+1];
s[i+1] = temp;
}
}
}
}
void sort_on_student_number(student s[], int n)
{
student temp;
int pass;
for(pass = 1; pass <= n-1; pass++)
{
for(int i = 0; i < n-pass; i++)
{
if(s[i].student_number > s[i+1].student_number)
{
temp = s[i];
s[i] = s[i+1];
s[i+1] = temp;
}
}
}
}
void sort_on_gpa(student s[], int n)
{
int i;
student temp;
for(int pass = 1; pass <= n-1; pass++)
{
for(i = 0; i < n-pass; i++)
{
if(s[i].gpa > s[i+1].gpa)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
double average_cal(student s[], int n)
{
double sum = 0, //local variable for sum calculation
av; //local variable for average calculation
for(int i = 0; i < n; i++)
{
sum = sum + s[i].gpa;
}
av = sum/n;
return av;
}