can't use getline for a file save function?

Okay so I'm working on this and for some reason I can't use getline in my save function, I switched it to a cin instead and it works just fine. I was wanting to know if there's a way to use a getline in the context I'm trying to use it. This isn't a threatening problem because as of now I can switch it out for cin, but I'm just curious on why it wasn't working. Also, if anybody knows a way I should better format the area where its inputting the player data would help, or if there's something I'm not thinking of in the first place.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Game::save()
{
	std::string filename;
	std::cout << "Choose a save file name." << std::endl;
	getline(std::cin, filename);

	std::ofstream playerfile;
	playerfile.open(filename);
	playerfile << "PLAYER DATA \n" << 
		player.getName() << "\n" <<
		player.getLevel() << "\n" <<
		player.getXp() << "\n" <<
		player.getXpNext() << "\n" <<
		player.getHp() << "\n" <<
		player.getHpMax() << "\n" <<
		player.getStamina() << "\n" <<
		player.getDamageMin() << "\n" <<
		player.getDamageMax() << "\n" <<
		player.getDefence();
	playerfile.close();
	std::cout << std::endl;
}
The problem with getline() is probably because somewhere prior to this function you have used the extraction operator >> which leaves the end of line character in the buffer. You will need to retrieve this new line character prior to the getline().


Topic archived. No new replies allowed.