fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct GameState {
  8. int score;
  9. // Add more game state variables as needed
  10. };
  11.  
  12. void saveGame(const GameState& state) {
  13. ofstream outFile("game_state.txt");
  14. if (outFile.is_open()) {
  15. outFile << state.score << endl;
  16. // Save more game state variables as needed
  17. cout << "Game saved successfully!" << endl;
  18. outFile.close();
  19. }
  20. else {
  21. cout << "Unable to save the game!" << endl;
  22. }
  23. }
  24.  
  25. bool askToQuit() {
  26. char choice;
  27. cout << "Do you want to quit the game? (y/n): ";
  28. cin >> choice;
  29. return (choice == 'y' || choice == 'Y');
  30. }
  31.  
  32. int main() {
  33. GameState currentState;
  34. currentState.score = 100; // Sample game state
  35.  
  36. if (askToQuit()) {
  37. saveGame(currentState);
  38. cout << "Exiting the game..." << endl;
  39. }
  40. else {
  41. cout << "Continuing the game..." << endl;
  42. // Add game logic here
  43. }
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.01s 5268KB
stdin
 
stdout
Do you want to quit the game? (y/n): Continuing the game...