OKAY...I was hoping to finish this assignment without any more help but after 4 hours of trying to find my problem I have gave in.
What I have is an Array of Structs that I need to sort, within the struct I have names stored in arrays and thats what I have to sort by, then I will have to change it to sort by the Enum within the struct as well but one thing at a time.
I just need it to sort the list first.
The prog reads information in from a file stores it in the Array of structs, prints the original list then sorts and prints the sorted list. Right now all it does it read store print original list and then prints the original list again unsorted. I cannot see where I have messed up so I am hoping a fresh set of eyes will see my error and let me know where it is and possibly what I have to do to correct it.
Code:
#include <string>
#include <iostream>
#include <cstring>
#include <cassert>
#include <fstream>
using namespace std;
enum year_in_school {freshman, sophomore, junior, senior, special};
struct studentrec
{
char name[20];
int age;
float gpa;
year_in_school classification;
}; //End Struct
istream &operator >>(istream &is, year_in_school &yr);
istream &operator >>(istream &is, studentrec &r);
ostream &operator <<(ostream &os, year_in_school yr);
ostream &operator <<(ostream &os, const studentrec &r);
bool operator <(studentrec st1, studentrec st2);
template <class t> void sortlist(t list[], int count);
int main(void)
{
studentrec stlist[17];
int i;
int stcount;
ifstream fin;
cout << "Enter the name of the input file: ";
string inputFileName;
getline(cin, inputFileName);
fin.open(inputFileName.data()); // Filestream Open
assert(fin.is_open()); // Check to make sure filestream is open
for (i=0; i<=17; i++)
fin >> stlist[i]; // Filestream reads into array of structs
if (fin.eof() );
fin.close(); // Filestream Closed
cout << "\n\nOriginal List of Students" << endl; //List before Sort
for (i=0; i<17; i++)
cout << stlist[i];
cout << "\n\nSorted List of Students" << endl; // List after Sort
sortlist (stlist, stcount);
for (i=0; i<17; i++)
cout << stlist[i];
system ("pause");
return 0;
}//end Main
istream &operator >>(istream &is, year_in_school &yr)
{
int year;
is >> year;
switch( year )
{
case freshman:
yr = freshman;
break;
case sophomore:
yr = sophomore;
break;
case junior:
yr = junior;
break;
case senior:
yr = senior;
break;
case special:
yr = special;
break;
default:
return (is);
break;
};
return( is );
} // end Year Switch
istream &operator >>(istream &is, studentrec &r)
{
int temp;
char filler[80];
is.get(r.name,20);
if(is.eof())
return is;
is >> r.age >> r.gpa >> r.classification;
is.getline(filler,80);
return is;
} //end Studentrec input
ostream &operator <<(ostream &os, year_in_school yr)
{
switch(yr)
{
case freshman:
os << "Freshman";
break;
case sophomore:
os << "Sophomore";
break;
case junior:
os << "Junior";
break;
case senior:
os << "Senior";
break;
case special:
os << "Special";
break;
default:
os << "Unspecified" << endl;
break;
}
return os;
} //end year_in_school OS
ostream &operator <<(ostream &os, const studentrec &r)
{
os << " Name : " << r.name << endl;
os << " Age : " << r.age << endl;
os << " GPA : ";
os.precision(4);
os << r.gpa << endl;
os << "Classification : " << r.classification << endl;
os << endl;
return( os );
} //end print function
bool operator <(studentrec st1, studentrec st2)
{
return (strcmp(st1.name, st2.name))<0;
} //end "<" define
template <class t> void sortlist(t list[], int count)
{
int minpos;
int i;
int pass;
count--;
for(pass=0; pass<count; pass++)
{
minpos = pass;
for(i=pass+1; i<=count; i++)
if (list[i] < list[minpos])
minpos = i;
if(minpos != pass)
swap(list[minpos], list[pass]);
}
return ;
} // End Sortlist
Also here is the text file that it reads input in from called "students.txt"
Code:
Ericson, Josh 21 3.513 3
Sampson, Lee 45 3.7 4
Thompson, Jim 18 2.5 9
Clark, James E. 21 3.75 1
Johnson, John T. 20 1.75 0
Bradley, Bill 19 2.1 1
Fitzgerald, Thomas 20 3.6 2
Zircon, Zelda 49 2.05 5
Brown, Susan 18 2.35 0
Collin, Amanda 19 2.95 1
Weatherington, Jane 37 1.95 5
Bullington, John 23 3.516 4
Limbaugh, Rush 55 0.75 5
Cuomo, Mario 71 2.98 5
Ford, John 17 0.00 0
Bush, Maynard 18 2.65 1
Maynard, Jill 22 3.15 3
Also DEV-C++ will not recognize the "system("pause");" command now...Last night before I added the sort it was working fine would read store print and stop, now it just compiles, executes and exits the prog,all I see is the prog window flash up on the screen...I tried commenting out all the sort stuff to see if that was it but not no matter what it exits and never pauses...Any ideas on that...Other progs that I have with the pause command work fine...it will stop on those but not this one.
Thanks in advance everyone.