Question 1: I want to create a save file for a game im creating in C++ problem is i can only use the vanilla headers in dev-cpp (which spawns another question id like to have answered/suggestions), my main problem id like to get past, is how the easiest format/way of the program creating multiple files, and reading from them. (Note: this will be a decent discussion, as im looking to learn and will most likely be asking 50 questions for every answer i recieve, as i am trying to program my first game in C++ that wasn't as simple as blackjack...(more time consuming than anything in that case, a fair bit of thinking and if/else/else if statements))

Question 2: as ive stated, i need to use a vanilla dev-Cpp as im sure my teacher (to which this program is being handed into, will most likely not have, or prefer i don;t complicate and use new headers(not to say im not already going a bit over and above with a save function in this case)) Actual Question: I need game ideas... Extra info/Description :A game that has no images is pretty boring, and a long text game takes far too long for the time i have (roughly 3-4 weeks, to hand in)my idea was mastermind, to the explanation goes as follows: MasterMind is a number game. The computer will select a secret number with five different (unique) digits. The object of the game is to guess that secret number. Each guess is answered by the number of digits in the guess number that match or occur in the secret number. You will also be told how many of the digits are in the correct position in the secret number. Through a process of elimination, you should be able to deduce the correct digits using logic.
For example:
If the secret number is "12359"
- you enter "11359"
The computer will report "Number Correct = 4" and "Correct Position = 4"
- you enter "55555"
The computer will report "Number Correct = 1" and "Correct Position = 1"

(copy and pasted from http://www.easysurf.cc/master3.htm)

So , to begin. im going to mention im pretty new to Coding, as im sure you could expect from my title. soo to get to the point... My thoughts lead me to using a counter in a loop, simply to read multiple files, and check the profile name pertaining to them, as to find the correct one, or what actually just came to mind which should work much better is just having them enter a profile name, first it searches for it, using filename, if it doesn't exist it creates a new one, if it does then it reads into it (i believe i already know how to create a password protection (very primatively however)).

thanks for reading, i know its a bit long, especially for the simplicity of the problem. However i can see it will be answered in multiple ways, and there is most likely easy ways to do certain things, moreso than i currently know.

also, tried to put this into the game development thread, but couldn't figure out how...

will post code snippets as soon as i organize and create a ramshackle attempt at the base of the save/load function and Game format

Recommended Answers

All 13 Replies

"vanilla headers in dev-cpp" I suppose you want to stick with the C++ Standard Library? Or are you allowed to use the windows API as well? You can't make a UI just by using standard C++ libraries, unfortunatly.

I'll assume you already know how to do file IO in C++.

There are plenty of simple formats you can use. If your using mastermind, you could just save the secret number on the first line, and the following lines can just be the user guesses.

CSV files are also usefull to know how to work with. You could do something like that.

Game ideas:
Standard C++ libraries:
(1) Tic-Tak-Toe.
(2) Connect four.
(3) Checkers/Chess.
(4) A card game/solitare game (FreeCell is my favourite).
(5) Suduko (generating suduko puzzles isn't as easy as it seems! have fun!)
(6) Chinchirorin
(7) Somethink like Zork.

Windows API:
(1) Any of the above but with a gui.
(2) A rogue like game with one level and simple controls. (ie, walk around, kill monsters by walking into it)
(3) The classic game of snake.
(4) Space invadors.
(5) Something like Chips challenge.

These all of course can be simplified down.

The computer will select a secret number with five different (unique) digits. The object of the game is to guess that secret number. Each guess is answered by the number of digits in the guess number that match or occur in the secret number. You will also be told how many of the digits are in the correct position in the secret number. Through a process of elimination, you should be able to deduce the correct digits using logic.

For example:

If the secret number is "12359"
- you enter "11359"
The computer will report "Number Correct = 4" 
and "Correct Position = 4"
- you enter "55555"
The computer will report "Number Correct = 1" 
and "Correct Position = 1"

I want to create a save file for a game im creating in C++ problem

What data do you wish to save?
Can you give an example of how your data file might look?

@david, the intent if i were to do that, is to include a counter/timer as well as decreasing time limits, so in that case i would be saving:

level(sets the time decrease increments) ,
progress(time left),
Username(would be the file name),
and for the hell of it
description(about themselves

this part is simple enough i belive, however i do hope to do a more inclusive part of this game, as in have 5 games, that all save progress, so it would 5 different classes, that store information for 5 different games. i should have a few to compile my save file template tonight, and have it posted for tomorrow. one thing im wondering if it would be easier and more logical to just make seperate files with Usernames as file Name, or just use the class structure to svae it all in a single binary file?

ok ...

could try (to start) something like...

fileName = "david_w.txt"

#intLevelNum intSecsLeft short text info re player
1            120         newbie 
1            30  
2            120
2            30
2            120
2            60
3            120

I would still probably go the cvs route:

name,level,progress,username,description
Chris,0,34,ChrisP-C++,Newbie Poster
Brandon,7,28,Hiroshe,Posting Whiz
David,1,31,David W,Posting Whiz in Training

It has the adgantage of being readibly parsable/importable in other languages/tools.

#include <iostream>
#include <fstream>
using namespace std;


void Save();
void Load();
class profile
{
      char Username [20];
      char Description [150];
      int Level, Progress;
};


int main ()
{

   Save ();

   return 0;
}

void Save ()
{
     ofstream myfile ("Savefile.cvs", ios::app | ios::binary);
     if (myfile.is_open()) 
     { 
     myfile << "testing\n";
     myfile << "column2,line2\n";
     myfile.close();
     }
     else {cout << "An error has occured";}

}

void Load ()
{

}

this so far is my very simple work in progress, the only problem im fighting with now. is that im attempting to make the file unable to open simply by changing it to .txt unfortunately the only thing binary does, is make it so that it cannot be opened in proper format if changed to .txt

a fair point to mention is that this is just my save option, i am working on the main game under another file for obvious reasons

Maybe it has something to do with line endings.

I'm not sure if you would want to be appending each time you open the file (unless you've designed a mechanism that requires data to be continuously appended).

What happens if you delete the save file, and run it like this::

ofstream myfile("Savefile.cvs");
if (myfile.is_open()) {
    myfile << "name,level,progress,username,description\n";
    myfile.close();
}
else {
    cout << "An error has occured";
}

oh, fair warning. im using dev-C++... not that it matters in this case... When i compile it as you've written, it writes to the file as you expect, (i just change the extension to txt to read it) however, i was appending the file, to do it as you showed earlier, because the idea here is that you play it once and it saves. Someone else plays on the same computer, it saves the same file under a different line, So that way when they enter their username, i can have statements search for the name in the first category, and then parse only that line back into the program. or would it be more logical to use that format, just create a different save file each time under the players username. So that when they log back in, it opens that file?

basically my 2 questions that remain here, are #1 how can i save the file so that only my program can open it, that way someone cant just change the ext and mess with it. as well as #2 whether i should save it all in one file, and have the programs load function sort to the correct line to parse, or just create seperate file names for each user to load up?

Oh I see what you mean. You renamed it to .txt so you could open it up in notepad. Yeah, writting it in binary isn't an easy idea - you need to be aware of the text encoding, the BOM and the file endings in order to do it properly. It's better to use the regular text mode.

If you just append the score each time you play, your file will grow and grow with useless data.

When the user logs in, it should open the file for reading, and read everything into a vector and retrieve the users information.

While the user is playing, it should update the information in the vector.

When the user exits, it should open the file for writting, and write the vector back into the file.

im not really too knowledgable of vectors and how to use them, but i do see exactly what you mean and i understand what you mean with the vector. in that case though, it would require that each user has his/her own file.

Is there any way of reading to a specific line (i understand using the getlinein a loop to find a line), and only delete that line of code. That way i can update it line by line..? i believe i can set where in the txt the save function writes to, im just not too sure how to delete from a file...

A vector is just a wrapper for an array that will automatically resize for you.

it would require that each user has his/her own file.

Huh? A vector wouldn't effect it. Just put all thr users in one file.

Is there any way of reading to a specific line

You can't really read/write an individual line - that's what the vector is for. You read the file into a vector (or anything really), and when your done making changes, you rewrite the file.

  1. Your user launches the game.
  2. The game reads Savefile.cvs into vector< vector<string> > saves or some other data structure (2D array is fine).
  3. The user logs in, and the program looks up the user in saves.
  4. The user plays the game, and their score is changed in saves for that user.
  5. The user logs out. The game writes saves into Savefile.cvs.

alright, im gonna have to do a little bit of reasearching before i return lol...

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.