Hello all,
So I am having trouble figuring out a runtime error I am getting in my code. Essentially I read in data and push it into two lists (using the STL list). Then I use the .sort() function to sort the data by average. However I am having a problem. When the list is printed in UNSORTED order, I get an exact copy of my data (a good thing). When I sort the list, one data element is left out and another is replicated ( not good)
Below I have the .dats (both input and output) along with the code to explain the error.
ANY HELP WOULD BE AMAZING the STL is kicking my butt.
Master.dat
jones 44
smith 74
thomas 88
ralph 56
sue 90
jane 73
mavis 77
joyce 33
pete 56
jacob 28
emily 99
michael 89
madison 45
joshua 85
hannah 92
matthew 10
emma 65
ethan 23
alexis 8
joseph 56
ashley 83
sorted .dat with error
alexis 8
matthew 10
ethan 23
jacob 28
joyce 33
jones 44
madison 45
ralph 56
pete 56
joseph 56
emma 65
jane 73
smith 74
mavis 77
ashley 83 <--- ashley is put in properly
ashley 83 <--- ashley is reinserted, not correct
joshua 85
thomas 88
michael 89
sue 90
hannah 92 <--- the last ele should be emily 99, however the extra ashley took it out
the source code:
/*******************************************************************************************
Devang N. Joshi *
CSCI 271 *
Assignment Three *
February 19th, 2011 *
STL Double Linked List *
*
The purpose of this assignment is to gain experiance using C++ built in List STL *
/******************************************************************************************/
#include <iostream> // |
#include <iomanip> // |
#include <fstream> // |
#include <cstdlib> // |
#include <cstring> // |
#include <cmath> // |
#include <list> // |
using namespace std; // |
//-----------------------------------------------------------------------------------------|
struct cProg3 // |
{ // |
char lname[21]; // |
float avg; // |
int gSize; // |
cProg3(); // |
void Read_m(fstream &Infile); // |
void Read_g(fstream &Infile);
bool operator < (const cProg3& other) const // |
{ // |
return (avg < other.avg); //compare by average. // |
}; // |
// |
// |
}; // |
/***Functions**/ // |
//-----------------------------------------------------------------------------------------|
cProg3::cProg3() // |
{ // |
strcpy(lname,""); // |
avg = 0; // |
gSize = 0; // |
} // |
//-----------------------------------------------------------------------------------------|
void cProg3::Read_m(fstream &Infile) // |
{ Infile>>lname>>avg; } // |
//-----------------------------------------------------------------------------------------|
void cProg3::Read_g(fstream &Infile) // |
{ Infile>>lname>>gSize; } // |
//-----------------------------------------------------------------------------------------|
/***MAIN***/ // |
//-----------------------------------------------------------------------------------------|
int main() // |
{ // |
//-----------------------------------------------------------------------------------------|
/***File Handlers***/ // |
fstream Infile; // |
fstream Outfile; // |
// |
/***Class Decleration***/ // |
cProg3 Prog3; // |
// |
/***List Decleration***/ // |
list<cProg3>theList; //the list for master.dat // |
list<cProg3>groupList; //the list for groups.dat // |
list<cProg3>::iterator it_m; //master list iterator // |
list<cProg3>::iterator it_g; //groups list iterator // |
list<cProg3>::iterator loopCount_m; //for read & write loops in master list // |
list<cProg3>::iterator loopCount_g; //for read & write loops in groups list // |
//-----------------------------------------------------------------------------------------|
/***Open Master Data and error check***/ // |
Infile.open("master.dat",ios::in); // |
if(!Infile) // |
{ // |
cerr<<"FILE NOT FOUND!!!!!!!!!!!!!"<<endl; // |
exit(1); // |
// |
} // |
else // |
{ // |
// |
cout<<"Master Data File Found & Opened"<<endl; // |
// |
/***Load Data into List***/ // |
while(!Infile.eof()) // |
{ // |
Prog3.Read_m(Infile); // |
theList.push_back(Prog3); // |
// |
};//while // |
// |
cout<<"Master Data File Loaded into List"<<endl<<endl; // |
Infile.close(); // |
// |
} // |
//-----------------------------------------------------------------------------------------|
/***Open Groups Data and error check***/ // |
Infile.open("groups.dat",ios::in); // |
if(!Infile) // |
{ // |
cerr<<"FILE NOT FOUND!!!!!!!!!!!!!"<<endl; // |
exit(1); // |
// |
} // |
else // |
{ // |
// |
cout<<"Groups Data File Found & Opened"<<endl; // |
// |
/***Load Data into Array***/ // |
while(!Infile.eof()) // |
{ // |
Prog3.Read_g(Infile); // |
groupList.push_back(Prog3); // |
// |
};//while // |
// |
cout<<"Groups Data File Loaded into List"<<endl<<endl; // |
Infile.close(); // |
// |
} // |
//-----------------------------------------------------------------------------------------|
/***Sort List***/
theList.sort();
// |
//-----------------------------------------------------------------------------------------|
/***Print Lists Back Out**/ // |
Outfile.open("output.dat",ios::out); // |
//-----------------------------------------------------------------------------------------|
/***Print Master List***/ // |
// |
loopCount_m = theList.end(); //sent loop counter to end of list // |
loopCount_m--; //move one position back for proper output // |
it_m = theList.begin(); //set iterator to begining of list // |
// |
while(it_m != loopCount_m) // |
{ // |
cout<<it_m->lname<<" "<<it_m->avg<<endl; // |
Outfile<<it_m->lname<<" "<<it_m->avg<<endl; // |
it_m++; // |
// |
};//end loop // |
cout<<endl<<endl; // |
Outfile<<endl<<endl; // |
//-----------------------------------------------------------------------------------------|
/***Print Group List***/ // |
// |
loopCount_g = groupList.end(); //sent loop counter to end of list // |
loopCount_g--; //move one position back for proper output // |
it_g = groupList.begin(); //set iterator to begining of list // |
// |
while(it_g != loopCount_g) // |
{ // |
cout<<it_g->lname<<" "<<it_g->gSize<<endl; // |
Outfile<<it_g->lname<<" "<<it_g->gSize<<endl; // |
it_g++; // |
// |
};//end loop // |
cout<<endl<<endl; // |
Outfile<<endl<<endl; // |
//-----------------------------------------------------------------------------------------|
Outfile.close(); // |
// |
return 0; // |
// |
}//end main // |
/******************************************************************************************/