Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 01-06-2007, 07:12 PM   #1 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default Problem with lists

In my program, I am trying to create a list of objects that contain a list of objects. I have a list of people, that each contain a list of surveys. Everything is fine, and I have no problem poplulating the lists using various functions, the problem is when I go to iterate and print out the properties. First let me mention that for each object, I have used a friend function to overload the extraction operator. Below is the function in which I am trying to print out each person in the personlist, and trying to access and print out each survey in the survey list (that the person object contains). Below is what I have tried with no luck. I created a function in the person.cpp that iterates through the list of surveys and requires a list<survey> as a parameter. It basically looks the same as the one below. Any ideas on how to go about this?

Code:
void EmployeeList::ViewEntries(list<Person> list)
{
             for(iter = list.begin(); iter != list.end(); ++iter)
	{
		cout << (*iter) << endl;
		//cout << (*iter).ViewSurveys((*iter).GetList()) << endl;
	}
}
Axle is offline   Reply With Quote
Old 01-06-2007, 07:55 PM   #2 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default

Actually, after a quick sandwich, soda, cigarette and bathroom break it occurred to me. In my viewsurveys function, I print out the objects (surveys) using the overloaded extraction operator, so all I have to do is call that function within the iteration of the personlist:

Code:
void EmployeeList::ViewEntries(list<Person> list)
{
	for(iter = list.begin(); iter != list.end(); ++iter)
	{
		cout << (*iter) << endl;
	        (*iter).ViewSurveys((*iter).GetList());
	}
}
I would still like to hear opinions and/or suggestions on better ways to do this.

EmployeeList.cpp

Code:
#include "Controller.h"
#include "EmployeeList.h"


list<Person> personList;
list<Person>::iterator iter;

EmployeeList::EmployeeList(const string &f):filename(f)
{
	cout << "Constructed a List" << endl;
	Sleep(500);
}
EmployeeList::~EmployeeList()
{
	// code to handle destruction
}
list<Person> EmployeeList::GetList()
{
	return personList; 
}
void EmployeeList::AddEntry()
{
	string name;
	int id;

	cout << "Please enter your name: " << flush;
	getline(cin, name);
	cout << "Please enter your 5 digit empoyee I.D.: " << flush;
	cin >> id;
	personList.push_back( *(new Person(id, name)) );
}
void EmployeeList::ViewEntries(list<Person> list)
{
	for(iter = list.begin(); iter != list.end(); ++iter)
	{
		cout << (*iter) << endl;
	             (*iter).ViewSurveys((*iter).GetList());
	}
}
Person.cpp

Code:
#include "Person.h"
#include "Controller.h"

Person::Person(int id, const string &n):name(n)
{
	empId = id;
	name = n;
	GetSurveyInfo();
}

Person::~Person()
{
}

ostream& operator<< (ostream& osObject, const Person& person)
{
	osObject << person.name << " | " << person.empId << endl << endl;
	return osObject;
}

list<Survey> Person::GetList()
{
	return surveyList;
}
void Person::GetSurveyInfo()
{
	string name;

	cout << "Enter the name of the survey: " << flush;
	cin.ignore();
	getline(cin, name);

	Survey survey = *(new Survey(name));
	cin >> survey;
	AddSurvey(survey);
}

void Person::AddSurvey(Survey survey)
{
	char ch;
	system("cls");
	cout << survey;
	cout << "Is this information correct? (Y)es/(N)o?" << endl;
	cin >> ch;

	if (ch == 'N' || ch == 'n')
	{
		GetSurveyInfo();
	}
	else if(ch == 'Y' || ch == 'y')
	{
		surveyList.push_back( survey );	
	}
	else
		AddSurvey(survey);

	Sleep(1000);
}
void Person::ViewSurveys(list<Survey> list)
{
	for(iter = list.begin(); iter != list.end(); ++iter)
	{
		cout << (*iter) << endl;
	}
}

Last edited by Axle; 01-06-2007 at 08:02 PM.
Axle is offline   Reply With Quote
Old 01-06-2007, 08:30 PM   #3 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

actually what you do is what you need. however, this is better in OOP sense.
Code:
void Person::ViewSurveys()
{
	for(iter = surveyList.begin(); iter != surveyList.end(); ++iter)
	{
		cout << (*iter) << endl;
	}
}
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 01-06-2007, 09:20 PM   #4 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default

I see what you're saying, plus it eliminates the need for the function getlist(). Thanks for the input.
Axle is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5