couple of problems

Hi!

I've been working on this assignment i have whole day and I'm a bit stuck now...

1.this is my function so i can remove a player from the array(array is unsorted)
1
2
3
4
5
6
7
8
9
10
11
void PlayerRegister ::removePlayer(string name, int dateOfBirth)
{
	for(int i=0;i<this->nrOfPlayers;i++)
	{
		if( Players[i]->getName()==name && Players[i]->getDateOfBirth()==dateOfBirth)
		{
			Players[i]=Players[this->nrOfPlayers];
			this->nrOfPlayers--;
		}
	}
}


It compiles and I'm able to remove someone and then when I use my function show all the program crashes.

2.I need to make a function toString() and load all my ints and strings in it and then return it.. have no idea how to do this...

3. I have a function that allowes me to save everything from my array to a .txt file but I dont know then how to make a function so I'll be able to read the info into an array from that same file..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void PlayerRegister::saveToFile(string filename)
{
	ofstream out;
	filename+=".txt";
	out.open(filename.c_str());
	for(int i=0;i<this->nrOfPlayers;i++)
	{
		out<<this->Players[i]->getName()<<endl;
		out<<this->Players[i]->getDateOfBirth()<<endl;
		out<<this->Players[i]->getPhoneNr()<<endl;
		out<<this->Players[i]->getJerseyNr()<<endl;
		out<<this->Players[i]->getPosition()<<endl;
	}
	out.close();
}




Any kind of help is very much appreciated!
thanks in advance!
1. Players[nrOfPlayers] goes out of bounds. The last element is Players[nrOfPlayers-1]. Or you could just swap lines 7 and 8. Note that you don't have to use this-> everywhere. Unless you find that more readable..

2. http://www.cplusplus.com/articles/numb_to_text/

3. Pretty much the same, just change ofstream to ifstream, << to >> and remove the endl things.
edit: didn't notice you were using get functions. Now you have a choice. Either make getMember() return a reference to the member (which would make get functions sort of useless, but most likely they already are, so you could just make themembers public.. Unless this is for school and private members is a requirement), or write a setMember(member) function and replace out << Players[i]->getMember() << endl; with MemberType temp; in >> temp; Players[i]->setMember(temp);
note: with strings you should use std::getline(in, str) instead of in >> str.
Last edited on
Topic archived. No new replies allowed.