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										// |
/******************************************************************************************/

Recommended Answers

All 4 Replies

ok nevermind i think i found a way to do it, here is the code i have to "make it work" I am still open to suggestions however if there is a way to "streamline"

/*******************************************************************************************
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.				// |
  	};										// |	
											// |
											// |
};		

struct cProg3Temp										// |
{											// |
	char lname[21];									// |
	float avg;									// |
	int gSize;
	float relativeDiff;								// |
	cProg3Temp();		
							// |
	bool operator < (cProg3Temp& other)  					// |
	{										// |
		return (relativeDiff < other.relativeDiff); //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; }								// |
//-----------------------------------------------------------------------------------------|
cProg3Temp::cProg3Temp()									// |
{											// |
	strcpy(lname,"");								// |
	avg = 0;									// |
	relativeDiff = 0;									// |
}					
					/***MAIN***/					// |
//-----------------------------------------------------------------------------------------|
int main()										// |
{											// |
//-----------------------------------------------------------------------------------------|
	/***File Handlers***/								// |
	fstream Infile;									// |	
	fstream Outfile;								// |
											// |
	/***Struct Decleration***/							// |
	cProg3 Prog3;
	cProg3Temp Prog3Temp;									// |
											// |
	/***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<cProg3Temp>tempList;		     //the temp list used to process the records// |
	list<cProg3Temp>::iterator tempIt1;
	list<cProg3Temp>::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;
	float compAvg;
	float tempAvg;
	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));
				strcpy(tempLname,it_m->lname);
				Prog3Temp.relativeDiff = tempDiff;
				tempAvg = it_m->avg;
				Prog3Temp.avg = tempAvg;
				strcpy(Prog3Temp.lname,tempLname);
				tempList.push_back(Prog3Temp);  //<---i want to do something like that....
				cout<<"Printing the unsorted 'new struct part one' from struct"<<endl;
				cout<<Prog3Temp.lname<<" "<<Prog3Temp.avg<<" "<<Prog3Temp.relativeDiff<<endl;
				Outfile<<Prog3Temp.lname<<" "<<Prog3Temp.avg<<" "<<Prog3.relativeDiff<<endl;
				it_m--;
			}
			if(loopCount_m != theList.end())
			{
				tempDiff = abs ( (compAvg) - (loopCount_m->avg));
				strcpy(tempLname,loopCount_m->lname);
				Prog3Temp.relativeDiff = tempDiff;
				tempAvg = loopCount_m->avg;
				Prog3Temp.avg = tempAvg;
				strcpy(Prog3Temp.lname,tempLname);
				tempList.push_back(Prog3Temp);  //<---i want to do something like that....
				cout<<"Printing the unsorted 'new struct part two' from struct"<<endl;
				cout<<Prog3Temp.lname<<" "<<Prog3Temp.avg<<" "<<Prog3Temp.relativeDiff<<endl;
				Outfile<<Prog3Temp.lname<<" "<<Prog3Temp.avg<<" "<<Prog3.relativeDiff<<endl;
				loopCount_m++;
			}
		
		}//for		
		cout<<"----------Now Sort the temp list---------"<<endl<<endl;
		Outfile<<"----------Now Sort the temp list---------"<<endl<<endl;
		tempList.sort();
		

	}//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										// |
/******************************************************************************************/

ok an amendment to my question, below is my code and the datafiles. What i want to do is check to see if the search size is larger then the actual length of the list. if it is, then just print out the list and do no more. else process the list as usuall. however i am having trouble getting that if to work. any clues?

/*******************************************************************************************
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.				// |
  	};										// |	
											// |
											// |
};		

struct cProg3Temp										// |
{											// |
	char lname[21];									// |
	float avg;									// |
	int gSize;
	float relativeDiff;								// |
	cProg3Temp();		
							// |
	bool operator < (cProg3Temp& other)  					// |
	{										// |
		return (relativeDiff < other.relativeDiff); //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; }								// |
//-----------------------------------------------------------------------------------------|
cProg3Temp::cProg3Temp()									// |
{											// |
	strcpy(lname,"");								// |
	avg = 0;									// |
	relativeDiff = 0;									// |
}					
					/***MAIN***/					// |
//-----------------------------------------------------------------------------------------|
int main()										// |
{											// |
//-----------------------------------------------------------------------------------------|
	/***Local Variables***/
	int mListSize=0;

	/***File Handlers***/								// |
	fstream Infile;									// |	
	fstream Outfile;								// |
											// |
	/***Struct Decleration***/							// |
	cProg3 Prog3;
	cProg3Temp Prog3Temp;									// |
											// |
	/***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>::iterator compIt_m;
	list<cProg3Temp>tempList;		     //the temp list used to process the records// |
	list<cProg3Temp>::iterator tempItone;
	list<cProg3Temp>::iterator tempIttwo;	// |
//-----------------------------------------------------------------------------------------|
	/***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***/
		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;
		mListSize = theList.size();
		cout<<mListSize<<endl<<endl;			// |
		Infile.close();								// |
											// |
	}										// |
//-----------------------------------------------------------------------------------------|
//-----------------------------------------------------------------------------------------|
	/***Sort List***/								// |
	theList.sort();									// |
//-----------------------------------------------------------------------------------------|											// 
	char tempLname[21];
	char currLname[21];
	int tempSize;
	float compAvg;
	float tempAvg;
	float tempDiff;
	int c = 0;

	Infile.open("groups.dat",ios::in);						// |
	if(!Infile)									// |
	{										// |
		cerr<<"FILE NOT FOUND!!!!!!!!!!!!!"<<endl;				// |
		exit(1);								// |
											// |
	}
	else
	{
		Outfile.open("output.dat",ios::out);
		Infile>>tempLname>>tempSize;
		/***Check if number to search is larger then actual master list**/
		if(tempSize > theList.size())
			{	
				it_m = theList.begin();
				cout<<"For student '"<<tempLname<<"',the "<<tempSize;
				cout<<" closest in grade are:"<<endl<<endl;
				Outfile<<"For student '"<<tempLname<<"',the "<<tempSize;
				Outfile<<" closest in grade are:"<<endl<<endl;
				for(int c = 0; c <= theList.size(); c++)
				{
					if(strcmp(it_m->lname,tempLname) !=0)
					{
						cout<<"		"<<"Student: '"<<it_m->lname;
						cout<<"'| With an average of: '"<<it_m->avg;
						cout<<"'| '"<<"0"<<"' point diffrence"<<endl;				// |
						Outfile<<"		"<<"Student: '"<<it_m->lname;
						Outfile<<"'| With an average of: '"<<it_m->avg;
						Outfile<<"'| '"<<"0"<<"' point diffrence"<<endl;
						it_m++;
					}
					else
					{
						it_m++;
					}
					
				}//for
			}//if
		/***Process records because record search size is < master list size***/
		while (!Infile.eof())
		{
			cout<<"For student '"<<tempLname<<"',the "<<tempSize;
			cout<<" closest in grade are:"<<endl<<endl;
			Outfile<<"For student '"<<tempLname<<"',the "<<tempSize;
			Outfile<<" closest in grade are:"<<endl<<endl;
			
			//*********************************************************
			
				it_m = theList.begin();
					
			//*********************************************************************************
				while(strcmp(tempLname,it_m->lname) !=0)
				{
					it_m++;
				}
				compAvg = it_m->avg;
				strcpy(currLname,it_m->lname);
				loopCount_m = it_m;
				compIt_m = loopCount_m;
				
				if(it_m != theList.begin()) //adjustment if search val is at head of list
				{
					it_m--;
				}
				if(compIt_m++ != theList.end())//adjustment if search val is at tail of list
				{
					loopCount_m++;
				}
				for(int i=0; i<tempSize; i++)
				{
					if((it_m != theList.begin()) || (strcmp(currLname,it_m->lname)!=0))
					{
						tempDiff = abs ( (compAvg) - (it_m->avg));
						strcpy(tempLname,it_m->lname);
						Prog3Temp.relativeDiff = tempDiff;
						tempAvg = it_m->avg;
						Prog3Temp.avg = tempAvg;
						strcpy(Prog3Temp.lname,tempLname);
						tempList.push_back(Prog3Temp);
						it_m--;
					}
					if((loopCount_m != theList.end()) || (strcmp(currLname,it_m->lname)!=0))
					{
						tempDiff = abs ( (compAvg) - (loopCount_m->avg));
						strcpy(tempLname,loopCount_m->lname);
						Prog3Temp.relativeDiff = tempDiff;
						tempAvg = loopCount_m->avg;
						Prog3Temp.avg = tempAvg;
						strcpy(Prog3Temp.lname,tempLname);
						tempList.push_back(Prog3Temp); 
						loopCount_m++;
					}
		
				}//for		

				tempList.sort();
				tempItone = tempList.begin();	//set iterator to begining of list	
				
				cout<<"		"<<"Student: '"<<tempItone->lname;
				cout<<"'| With an average of: '"<<tempItone->avg;
				cout<<"'| '"<<tempItone->relativeDiff<<"' point diffrence"<<endl;				// |
				Outfile<<"		"<<"Student: '"<<tempItone->lname;
				Outfile<<"'| With an average of: '"<<tempItone->avg;
				Outfile<<"'| '"<<tempItone->relativeDiff<<"' point diffrence"<<endl;			// |
				for(int i=1; i<tempSize; i++)	
				{
					tempItone++;
					cout<<"		"<<"Student: '"<<tempItone->lname;
					cout<<"'| With an average of: '"<<tempItone->avg;
					cout<<"'| '"<<tempItone->relativeDiff<<"' point diffrence"<<endl;				// |
					Outfile<<"		"<<"Student: '"<<tempItone->lname;
					Outfile<<"'| With an average of: '"<<tempItone->avg;
					Outfile<<"'| '"<<tempItone->relativeDiff<<"' point diffrence"<<endl;				// |
				
				}// end for
				cout<<endl;
				Outfile<<endl;
				Infile>>tempLname>>tempSize;
				tempItone = tempList.begin();	//set iterator to begining of list
				tempIttwo = tempList.end();	//set iterator to end of list
				tempList.erase(tempItone,tempIttwo);	//erase the temp list for next pass
			
			
			
		}//while		

	}//master else	
	Outfile.close();								// |
											// |
	return 0;									// |
											// |
}//end main										// |
/******************************************************************************************/

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

groups.dat

smith 5
ralph 6
sue 2
mavis 8
joyce 3
jacob 2
emily 9
hannah 7
matthew 1
ethan 6
alexis 15
ashley 23

Ok... what's wrong with the actual code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.