The idea is that when the user dies he enters his name and it is saved along with his score keeping the names and scores that were already saved. the problem is that the code I have only overwrites the previous file and saves the last name entered. Probe opening the file and roasting all the data inside it to an auxiliary string but the only way to read data from a text file that I know is .getline () and this function uses char instead of string
this is the code:
when the player dies, the name is entered.
if (aircraft.isDead(false)){
bool flag = false;
text.setString("Juego Terminado \n Ingresa tu nombre:");
text.setPosition(200, 200);
if (event.type == sf::Event::TextEntered){
if (event.text.unicode < 128 && event.text.unicode != 13){
name.push_back(static_cast<char>(event.text.unicode));
playerName.setPosition(200, 300);
playerName.setString(name);
}
if (event.text.unicode == 13){
Save(name);
}
}
}
Save function:
void Game::Save(std::string nombre){
std::ofstream archivo("Puntajes.txt");
if (archivo.is_open()){
aircraft.saveScore(archivo, nombre);
}
archivo.close();
}
the saveScore function of the aircraft class:
void Aircraft::saveScore(std::ofstream &archivo, std::string nombre){
archivo << '\n'+nombre+": "+scorestr;
}