You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
void savePlayerData(const std::string& playerName, int playerScore) {
std::ofstream outputFile("playerData.txt");
if (outputFile.is_open()) {
outputFile << playerName << std::endl;
outputFile << playerScore << std::endl;
outputFile.close();
std::cout << "Player data saved successfully." << std::endl;
} else {
std::cout << "Error: Unable to open file for writing." << std::endl;
}
}
void loadPlayerData(std::string& playerName, int& playerScore) {
std::ifstream inputFile("playerData.txt");
if (inputFile.is_open()) {
std::getline(inputFile, playerName);
inputFile >> playerScore;
inputFile.close();
std::cout << "Player data loaded successfully." << std::endl;
} else {
std::cout << "Error: Unable to open file for reading." << std::endl;
}
}
int main() {
std::string playerName;
int playerScore;
// Save player data
std::cout << "Enter player name: ";
std::getline(std::cin, playerName);
std::cout << "Enter player score: ";
std::cin >> playerScore;
savePlayerData(playerName, playerScore);
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Snippit from GPT
#include
#include
#include
void savePlayerData(const std::string& playerName, int playerScore) {
std::ofstream outputFile("playerData.txt");
if (outputFile.is_open()) {
outputFile << playerName << std::endl;
outputFile << playerScore << std::endl;
outputFile.close();
std::cout << "Player data saved successfully." << std::endl;
} else {
std::cout << "Error: Unable to open file for writing." << std::endl;
}
}
void loadPlayerData(std::string& playerName, int& playerScore) {
std::ifstream inputFile("playerData.txt");
if (inputFile.is_open()) {
std::getline(inputFile, playerName);
inputFile >> playerScore;
inputFile.close();
std::cout << "Player data loaded successfully." << std::endl;
} else {
std::cout << "Error: Unable to open file for reading." << std::endl;
}
}
int main() {
std::string playerName;
int playerScore;
// Save player data
std::cout << "Enter player name: ";
std::getline(std::cin, playerName);
std::cout << "Enter player score: ";
std::cin >> playerScore;
savePlayerData(playerName, playerScore);
// Load player data
std::cout << "Loading player data..." << std::endl;
loadPlayerData(playerName, playerScore);
std::cout << "Player name: " << playerName << std::endl;
std::cout << "Player score: " << playerScore << std::endl;
return 0;
}
Beta Was this translation helpful? Give feedback.
All reactions