I cant seem to save a file.

Hello Everyone. Im doing an assignment and is only 1 little step away from finishing it. Basically. I read a file called "players.txt" using ifstream. I read in all the info and then I put them in a function which is in another class called toString, where I print them all out in a specific way.

Now, I have a switch statement. If they choose option "2" then all players will be printed out. Then they can choose option "3" and then can add a Match date to a certain player, if they then press "2" again, you can see that the match date has been added. The only thing missing is to actually save that new info, basically the entire thing to the file.

In the class, I have a function called

void save(ofstream* out);
 
void save(ofstream* out);


Which does this

1
2
3
4
5
6
7
8
9
10
void Player::save(ofstream* out)
    {
	
	*out << firstName << endl <<  lastName << endl << birthYear << endl;

	for (int i = 0; i < numberOfMatches; i++)
	{
		*out << matchDates << endl;
	}
    }


It saves everything into the *out. But I need to, from my switch statement, give it this "out". And I cant seem to figure out how.

Ive tried multiple things. One of them being this :
1
2
3
4
case 4:
	ofstream open("player.txt");
	team->save(open);
	break;


But it says it cant convert ofstream to *ofstream. Any help would be appricaited, tell me if you need more info :) thank you!
Last edited on
The fix is trivial - your save function takes a pointer to an ofstream, but open is an ofstream object. You need to pass the address of open into your function. As I'm sure you know (since you're using pointers in the first place), that means &open.

However, I'd have to question why you're using a pointer here in the first place. Why not simply have your function take a reference, instead?
Last edited on
However, I'd have to question why you're using a pointer here in the first place. Why not simply have your function take a reference, instead?


Only because we got a finished class from the professor, and in that class there was a pointer. I would have done what you suggested if it was up to me.

I tried what you suggested, is there any reason as to why all the dates that used to be numbers change into adresses after?

I have succesfully saved the file!! But the problem is that The adresses are saved rather what I type in. for example

William
Li
1989
2
20150131
20150201

changes to -

William
Li
1989
00BFBBFC
00BFBBFC

any idea why?
Last edited on
Not from the code you've posted. How are you defining matchDates ?
This is how I read the match dates -

1
2
3
4
5
6
7

for (int k = 0; k < numberOfMatches; k++)
			{
				string date;
				playerFile >> date; // date = 20150131
				team[i].addMatchDate(date);
			}

And this is the function -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Player::addMatchDate(string date)
{
	string* newMatchDates = new string[numberOfMatches + 1];

	if(matchDates != NULL)
	{
			for(int i = 0; i < numberOfMatches; i++)
			{
				newMatchDates[i] = matchDates[i];
			}

			delete[] matchDates;
	}

	newMatchDates[numberOfMatches] = date;

	matchDates = newMatchDates;
	numberOfMatches++;
}


Not sure if this is relevant but in the constructor matchDates is set to NULL.
Last edited on
Not sure why you're getting the error you're getting.

Edit: You're still not showing us how you're defining matchDates, but from context, I assume it's just another string*. If so, then there should be no confusion as to why it's displaying an address - an address (i.e. matchDates) is exactly what you're telling it to display.

I'm guessing you meant it to display matchDates[i].

Is there any reason you're using a C-style array and resizing the memory manually, rather than just using a std::vector and having it all done for you automatically?
Last edited on
I'm guessing you meant it to display matchDates[i].


That worked, thank you very much for everything :)

We're learning about vectors next week. We are using Arrays at the moment so we can "get to know them" a bit better.
You're welcome - glad it worked out!
Topic archived. No new replies allowed.