I/O - Get Data on selected lines?

hi, I'm learning cpp for about 2 weeks now and I wanted to try my skills, so I started to create custom "console game", unfortunatelly, I know how to save data in txt file, I know how to get data from that file, but it's not very effective to have 20 files (level.txt, name.txt etc) for one game, so I wanted to make one file where I will add everything, it would looks like

everything.mtg (MyTestGame)
1
2
3
4
5
6
7
8
9
10
11
12
Mekkatorqu
---Level
1
---CurrentXP
1
---Money
0
---Stats
200 // hp
200 // mana
10 // min damage
12 // max damage


those comments are only for now, so I wanna know if it is possible to make program find "Level", then go 1 line down and take that integer, I will be very happy if someone could help me, how I said, I'm learning cpp for about 2 weeks so I'm not very good at this, here is what I have done now:

-- sorry for my english :)

player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
#ifndef PLAYER_H
#define PLAYER_H 1

class player
{
public:
	player();
	int getLevel();
	int getXp();
	int getMaxLevel();
	int getMaxXpByLevel();
protected:
	int Level;
	int Xp;
	int MaxLevel;
	int MaxXpByLevel;
};

#endif 


player.cpp
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
#include "stdafx.h"
#include "player.h"
#include <sstream>
using namespace std;

player::player()
{
}

int player::getLevel()
{
	ifstream qLevel("Y2xldmVs.mtg");
	string qNewLevel;

	if(qLevel)
	{
		while (!qLevel.eof())
		{
			getline(qLevel, qNewLevel);
			stringstream ss(qNewLevel);
			ss >> Level;
		}
		qLevel.close();
	}
	else if(!qLevel)
	{
		ofstream NewFile("Y2xldmVs.mtg", ios::out);
		NewFile << "1";
		Level = 1;
	}
	return Level;
}

int player::getXp()
{
	ifstream qXp("Y3hw.mtg");
	string qNewXp;

	if(qXp)
	{
		while(!qXp.eof())
		{
			getline(qXp, qNewXp);
			stringstream ss(qNewXp);
			ss >> Xp;
		}
		qXp.close();
	}
	else if(!qXp)
	{
		ofstream NewFile("Y3hw.mtg", ios::out);
		NewFile << "1";
		Xp = 1;
	}
	return Xp;
}


main.cpp
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
#include "stdafx.h"
#include "player.h"
using namespace std;

void main()
{
	char cStartChar;
	player player;
	cout << "S - Start Game\nI - Info about character\nC - Credits\nQ - Quit Game\n\n";
	cout << "Enter a Char: ";
	cin >> cStartChar;
	if(cStartChar == 'S' || cStartChar == 's')
	{
		system("cls");
		cout << "Game is still in development" << endl;
	}
	else if(cStartChar == 'I' || cStartChar == 'i')
	{
		system("cls");
		cout << "Current Level: " << player.getLevel() << endl << "Current XP: " << player.getXp() << endl;
	}
	else if(cStartChar == 'C' || cStartChar == 'c')
	{
		system("cls");
		cout << "Coded by WodaN\'" << endl;
	}
	else if(cStartChar == 'Q' || cStartChar == 'q')
	{
		system("cls");
	}
	system("pause");
}
Last edited on
I have an idea for this:

Your Text File:

Level
1

CurrentXP
1

Money
0

Stats
200 // hp
200 // mana
10 // min damage
12 // max damage




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
int player::getLevel()
{
	ifstream qLevel("Y2xldmVs.mtg");
	string qNewLevel;

	if(qLevel)
	{
		while (!qLevel.eof())
		{
	           getline(qLevel, qNewLevel);
                   if(qNewLevel == "Level")
                    {
                       getline(qLevel, qNewLevel);
			 stringstream ss(qNewLevel);
			  ss >> Level;
                     }
		}
		qLevel.close();
	}
	else if(!qLevel)
	{
		ofstream NewFile("Y2xldmVs.mtg", ios::out);
		NewFile << "1";
		Level = 1;
	}
	return Level;
}



Now if you think about it the program will scan the text file for the word "level" then get the next line and put the number that appears there into the Level integer you could do the same for stats and stuff, hopfully that has pushed you in the right direction (might not give a permanent solution though
Last edited on
thanks, it works, but what about "stats" ? how can I set HP to int hp, mana to int mana etc?

btw, now I'm trying to write everything to one file, with different commands, I have this:

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
#include "stdafx.h"
#include "player.h"
#include <sstream>
using namespace std;

player::player()
{
}

int player::getLevel()
{
	fstream qData("data.mtg", ios::ate | ios::app);
	string qNewLevel;
	getline(qData, qNewLevel);
	if(qNewLevel == "Level")
	{
		getline(qData, qNewLevel);
		stringstream ss(qNewLevel);
		ss >> Level;
	}
	else
	{
		string qNewLevel("Level\n1\n\n");
		qData << qNewLevel;
		Level = 1;
	}
	qData.close();
	return Level;
}

int player::getXp()
{
	fstream qData("data.mtg", ios::ate | ios::app);
	string qNewXp;
	getline(qData, qNewXp);
	if(qNewXp == "XP")
	{
		getline(qData, qNewXp);
		stringstream ss(qNewXp);
		ss >> Xp;
	}
	else
	{
		string qNewXp("XP\n1");
		qData << qNewXp;
		Xp = 1;
	}
	qData.close();
	return Xp;
}


unfortunatelly, it's not writing things in file, just setting Xp or Level to 1, any suggestions?
Last edited on
Topic archived. No new replies allowed.