I feel bad for asking all the questions about the STL list, and thanks to those who have been helping me learn. I have a question now about inserting again, except this time I want to insert an extra data point into a tempList. I have commented out the code to explain what i am doing, any clues out there?
thanks in advance
/*******************************************************************************************
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;
float relativeDiff; // |
cProg3(); // |
void Read_m(fstream &Infile); // |
void Read_g(fstream &Infile);
// |
bool operator < (cProg3& other) // |
{ // |
return (avg < other.avg); //compare by average. // |
}; // |
// |
// |
}; // |
/***Functions**/ // |
//-----------------------------------------------------------------------------------------|
cProg3::cProg3() // |
{ // |
strcpy(lname,""); // |
avg = 0; // |
gSize = 0;
relativeDiff = 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>::iterator it_m; //master list iterator // |
list<cProg3>::iterator loopCount_m; //for read & write loops in master list
list<cProg3>tempList; //the temp list used to process the records// |
list<cProg3>::iterator tempIt1;
list<cProg3>::iterator tempIt2; // |
//-----------------------------------------------------------------------------------------|
/***Open Master Data and error check***/ // |
Infile.open("/home/win.thackerw/271/peer/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***/
Prog3.Read_m(Infile); // |
while(!Infile.eof()) // |
{ // |
theList.push_back(Prog3);
Prog3.Read_m(Infile); // |
// |
};//while // |
// |
cout<<"Master Data File Loaded into List"<<endl<<endl; // |
Infile.close(); // |
// |
} // |
//-----------------------------------------------------------------------------------------|
//-----------------------------------------------------------------------------------------|
/***Sort List***/ // |
theList.sort(); // |
//-----------------------------------------------------------------------------------------| //
char tempLname[21];
int tempSize;
int compAvg;
float tempDiff;
Infile.open("/home/win.thackerw/271/peer/groups.dat",ios::in); // |
if(!Infile) // |
{ // |
cerr<<"FILE NOT FOUND!!!!!!!!!!!!!"<<endl; // |
exit(1); // |
// |
}
else
{
Outfile.open("output.dat",ios::out);
Infile>>tempLname>>tempSize;
it_m = theList.begin();
while(strcmp(tempLname,it_m->lname) !=0)
{
it_m++;
}
compAvg = it_m->avg;
loopCount_m = it_m;
it_m--;
loopCount_m++;
for(int i=0; i<tempSize; i++)
{
if(it_m != theList.begin())
{
tempDiff = abs ( (compAvg) - (it_m->avg));
Prog3.relativeDiff = tempDiff;
//tempList.push_back(it_m->lname,it_m->avg,Prog3.relativeDiff); <---i want to do something like that....
cout<<it_m->lname<<" "<<it_m->avg<<" "<<Prog3.relativeDiff<<endl;
Outfile<<it_m->lname<<" "<<it_m->avg<<" "<<Prog3.relativeDiff<<endl;
it_m--;
}
if(loopCount_m != theList.end())
{
tempDiff = abs ( (compAvg) - (loopCount_m->avg));
Prog3.relativeDiff = tempDiff;
//tempList.push_back(loopCount_m->lname,loopCount_m->avg,Prog3.relativeDiff); <---i want to do something like that....
cout<<loopCount_m->lname<<" "<<loopCount_m->avg<<" "<<Prog3.relativeDiff<<endl;
Outfile<<loopCount_m->lname<<" "<<loopCount_m->avg<<" "<<Prog3.relativeDiff<<endl;
loopCount_m++;
}
}//for
cout<<"-------------------------------"<<endl<<endl;
Outfile<<"-------------------------------"<<endl<<endl;
}//else
//-----------------------------------------------------------------------------------------|
/***Print Lists Back Out**/ // |
// |
//-----------------------------------------------------------------------------------------|
/***Print Master List***/ // |
// |
it_m = theList.begin(); //set iterator to begining of list
// |
while(it_m != theList.end()) // |
{ // |
cout<<it_m->lname<<" "<<it_m->avg<<endl; // |
Outfile<<it_m->lname<<" "<<it_m->avg<<endl; // |
it_m++; // |
// |
};//end loop // |
//-----------------------------------------------------------------------------------------|
//-----------------------------------------------------------------------------------------|
Outfile.close(); // |
// |
return 0; // |
// |
}//end main // |
/******************************************************************************************/