Sign in to follow this  

Saving Option

This topic is 4139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am making a DOS game and I was wondering.. how would I make it if a user presses the S key then it will save their progress? IDE: Dev-C++ Language: C++

Share this post


Link to post
Share on other sites
If this is a console program, than I would wager to say that the only time the program pauses is when it waits for user input. In this case, the easiest (but perhaps not the most elegant) solution then would be to provide an if statement to check if the user wants to save instead of continuing.

Basically, every time you read input, you have to remember to perform this check. Which is why it's somewhat inelegant; but the only other solution I can think of is to use multithreading, and have one thread be devoted entirely to catching the 's' key. But that opens up a whole other can of worms: how would you know that the 's' keypress represents a request to save, and not simply input to a prompt? How would you stop the main thread from continuing if there is a request to save? etc. etc.

So, yeah, just stick to the first option [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Chad Smith
You would use File I/O. When the user presses s, you would save the important variable information to a file.

If you need an example, then just ask.

Chad


Can I get an example please? :P

Share this post


Link to post
Share on other sites
To use file i/o in C++, you will need to use ifstream (for reading) and ofstream(for writing), a very simple example:


#include <fstream> // for ifstream and ofstream
#include <iostream>
// creates an ofstream object associated with somefile.txt and open the file:
std::ofstream output("somefile.txt");
if (!output)
{
std::cout << "Oh no, an error occured!";
//do something about it
}
output << "this text will now be in somefile.txt";
output.close(); // file will be closed when output gets destroyed on scope exit, this will close it right now.



ifstream is similar.

Another option is to use xml, which I find personally easy to use for these purposes. This way you don't have to deal with a lot of issues when reading a file. There is a fine and small library for this, TinyXml, and the xml part of this gamedev article might help you.

Share this post


Link to post
Share on other sites

This topic is 4139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this