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");
}
|