saving and loading text game

I just got "Sam's teach yourself c++ in one hour a day" for Christmas and its taught me quite a bit, however, I looked ahead in the book and it doesn't seem to cover text based games, I'm working on one and improving it as I read, but I want to be able to save, load, and erase the process done in game. It wouldn't be used right away, I'd program it in then comment it out for when I need it, but it would be nice to know how. Hope someone answers :)
I also grabbed that book when i first started to learn programming. But that was when i was naive enough to think that i could actually learn c++ within a couple months. Don't let the title fool you. It takes a long time of practice to understand concepts, basic flow of a program, etc. Even people with 10+ years experience here say they are still learning and will always be learning.
http://norvig.com/21-days.html

Just because it does not explicitly say how to make a text game, does not mean that you can not use the advice from the book to make one.

To save, load, etc.for example: you can save to a text file, to load you can load a text file, and to erase you can just delete the text file, This will save your data and be able to retrieve it later when the program closes. This is somewhat like making your own database, and making your own way of saving and retrieving from your own database. There are more efficient ways like MySQL, but for a text based game beginner, i would stick to file input/output as it also helps you learn that basic concept also.

http://www.cplusplus.com/doc/tutorial/files/
Last edited on
I know that it isn't possible to learn in a matter f months, I wanted to get this book as a way to get started, so far its helped :) as for the writing text files and stuff, how do I do that?

http://www.cplusplus.com/doc/tutorial/files/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>

	
using namespace std;
void write (){
	ofstream filer;
	filer.open ("test.txt");
	filer <<  "Hey this is my first written text file. POOP\n";
	filer.close();
}

void read(){ //get up to certian chars
	char line[20];
	ifstream filer;
	filer.open("test.txt");
	filer.getline(line, 20);
	filer.close();
	cout << line;
}

void read2(){ //get entire line
	string s = "";
	ifstream filer;
	filer.open("test.txt");
	while(getline(filer, s)){
		cout << s <<endl;
	}
}

int main(){
	write();
	read2();
}
Last edited on
Alright, cool :) how do I use that to save the game? I can't seem to figure that out, I've only been coding for about a week :3
you make your own function to create the text file layout, and extract it the same.

for example:
this:
filer << "Hey this is my first written text file. POOP\n";

could be:
filer << "10 3 5";
where your function knows the the first number is (for example) 10 lives, and the second number 3 as hearts, and 5 something else whatever.
So your function split the line by whitespace and puts it in an arrayknowing the indexes as specific meaning.

at that point your functions returns those values back at the start of the game (which simulates the act of loading the game) as it knows when you left off or last saved.

There are a few more things in between, but you'll have to figure them out yourself.
Last edited on
Lol I'm confused, I'll give it a while(3-4 weeks) and if I don't got it by then, I'll have more questions xP thanks for the help
Have you tried to create anything yet?
i agree with pogrady.

Don't start off by trying to make a text game, start off with some of these exercises
http://www.cplusplus.com/forum/articles/12974/

spend a few days doing each, after you succeed, manipulate the program to do something else, to add 1 thing at a time. Then when you feel comfortable with that one move on to the next. Those alone could take awhile. Don't try to make something like Zork on your first week.
Lol I'm not trying anything like zork
hi,
i'm also doing a text base game.step by step. the code is show below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string> //this will enable to read char as a string 
#include <stdlib.h> // this will enable to generate random number
#include <time.h>

/*Every time you generate a random number in windows, 
it uses a pre-determined algorithm to produce the number.
This means the "Random" number will be the same every time you run your program.
In order to make your "random" number truely random,
we will use the current time as the random seed and then use some basic algebra
to reduce the number down to the range we need it in.*/

using namespace std;

string name;
int player_life = 100;
int monster_life = 100;

int monster_attack ()
{
	srand ( time(NULL) ); //initialize the random seed
	int damage = (rand ()%20) +1;
	player_life = player_life - damage;
	cout << "You have taken a hit." << "Damage: " << damage << endl;
	if (player_life < 0)
	{
		cout << endl;
	}
	else
	{
		cout << "You life is now " << player_life << endl;
	}
	return 0;
}

int player_attack ()
{
	srand ( time(NULL) ); //initialize the random seed
	int damage = (rand ()%20) +1;
	monster_life = monster_life - damage;
	cout << "The monster have taken a hit." << "Damage: " << damage << endl;
	if (monster_life < 0)
	{
		cout << endl;
	}
	else
	{
		cout << "The monster life is now " << monster_life << endl;
	}
	return 0;
}

int main ()
{
	cout << "Enter Your Name: ";
	cin >> name;
	cout << "Welcome " << name << endl;
	while (player_life > 0 && monster_life > 0)
	{
		monster_attack ();
		player_attack ();
		if (player_life < 0)
		{
			cout << "You have been killed by the monster." << endl;
		}
		else if (monster_life < 0)
		{
			cout << "You have kill the monster." << endl;
			cout << "You have save the world." << endl;
		}
	}
	system ("PAUSE");
	return 0;
}


i have problem with the random number generater. wonder anyone can help to improve it?
thanks.
Topic archived. No new replies allowed.