Is there a way to save in a text based game through the MSDOS window? I want to be able to save the variables that store the mana, health, ect of a player.

Recommended Answers

All 11 Replies

Sure.
All you need to decide is which variables you have in your game which are necessary to restore the state of the game.

You would use ifstream to read data (much like cin), and ofstream to write it (like cout). (note: cout and cin are objects of classes that are already made. ifstream and ofstream are actual classes).

To write data:

ofstream fout("file.sav");
fout << health << "\n" << mana << "\n" << etc;

to read data:

ifstream fin("file.sav");
fin >> health >> mana >> etc;

or make one class game_state and then output.write(&game_state, sizeof(game_state)) and to restore input.read(&game_state, sizeof(game_state))

If their stats are increasing as they progress through the game, how do you write it to the save file when they tell it to?

Er, there seem to be some awfully large gaps in your basic C++ knowledge to be writing a game.

File I/O is the first thing you typically learn how to do.

Er, there seem to be some awfully large gaps in your basic C++ knowledge to be writing a game.

File I/O is the first thing you typically learn how to do.

It wasn't for me.

Alright thanks, but what directive do you use to use ofstream and ifstream? If their stats are increasing as they progress through the game, how do you write it to the save file when they tell it to?

The code I showed you

1.
      ofstream fout("file.sav");
   2.
      fout << health << "\n" << mana << "\n" << etc;

is all you need to write the variables health, mana, and etc to a file. If you want to add more/different variables (im assuming you do), just add them following the above pattern. Make sure to #include <fstream> at the top of your file.

If you want to be able to save whenever you want to, all you need to do is make a save 'function'. Modularizing your code will make it much easier to work with, change, and improve. Say you have a struct 'hero' with variables strength, health, and exp. Your program might look something like this:

#include <iostream>
#include <fstream> //necessary for file i/o
using namespace std; 

struct hero {
  int str, hp, exp;
}

void saveGame(struct hero Zotan);

int main() {
  hero Zotan;
  Zotan.str = 10; Zotan.hp = 100; Zotan.exp = 0;
  char input;

  do { //this is a basic menu

    cout << "Choose an Action:\n
                  S - Save\nQ - Quit Game\n\n";
    cin >> input;
    switch(input) {
      case 'S':
        saveGame(Zotan);
        break;
      case 'Q':
        break;
    }
  }while(input != 'Q');

return 0;
}

void saveGame(struct hero Zotan) {
  ofstream saveData;
  saveData.open("gamedata.txt", ios::out);
  saveData << Zotan.str << "\n" << Zotan.hp << "\n" << Zotan.exp;
  saveData.close;
}

And there it is, basic file saving whenever you want. If you haven't yet read up on file i/o. It is very important stuff to know. Remember, modularizing is the key to a good program.
P.S. I'm not sure if that code will compile, so don't just copy it onto your developer. I wrote for you to use as a reference.
P.S.S. Im only 16 so don't criticize me too bad; I've been programming less than a year but I've got the basics down. Thanks everyone!

I did get the whole saving thing figured out but your way is definitely a different way than I did.

Thanks

I'm not exactly sure, but I think now you mark this thread as "solved", and then it says I'm more helpful at solving problems. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.