[Win32 Console] broken integer?

Hi guys, I have little problem with my program, I've started to learn c++ and now I wanna make custom "game" in console, unfortunatelly, I'm having problem, after calling player.load(); from player.cpp it should make Level = 10 and XP = 10 000 (data.txt), but after calling Level and XP in main.cpp they are same numbers (10, like Level)

screen:
http://img228.imageshack.us/i/beznzvucy.jpg/

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
33
34
#include "StdAfx.h"
#include "player.h"
using namespace std;

void main()
{
	int Level, Xp;
	char cStartChar;
	player player;
	player.Load();
	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: " << Level << endl << "Current XP: " << Xp << endl;
	}
	else if(cStartChar == 'C' || cStartChar == 'c')
	{
		system("cls");
		cout << "Coded by WodaN\'" << endl;
	}
	else if(cStartChar == 'Q' || cStartChar == 'q')
	{
		system("cls");
	}
	system("pause");
}


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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "StdAfx.h"
#include <cstdlib>
#include "player.h"
#include <sstream>
using namespace std;

player::player()
{
}

void player::Load()
{
     fstream lData("Data.txt");
     string qNewXp;
     bool xpf = false;
     if(lData.good())
     {
     while(!lData.eof())
		{
			getline(lData, qNewXp);
			if(qNewXp == "XP")
			{
				getline(lData, qNewXp);
				stringstream ss(qNewXp);
				ss >> Xp;
				xpf = true;
				lData.close();
			}
			else if(lData.eof() && xpf == false)
			{
                 lData.close();
				ofstream lData("Data.txt", ios::app);
				lData << "XP\n1\n";
				Xp = 1;
				xpf = true;
				lData.close();
			}
		}
	}
	else
	{
        lData.close();
		ofstream lData("Data.txt", ios::app);
		lData << "XP\n1\n";
		Xp = 1;
	    lData.close();
	}
	lData.close();
     fstream qData("Data.txt");
     string qNewLevel;
     bool lvlf = false;
     if(qData.good())
     {
     while(!qData.eof())
		{
			getline(qData, qNewLevel);
			if(qNewLevel == "Level")
			{
				getline(qData, qNewLevel);
				stringstream ss(qNewLevel);
				ss >> Level;
				lvlf = true;
				qData.close();
			}
			else if(qData.eof() && lvlf == false)
			{
                 qData.close();
				ofstream qData("Data.txt", ios::app);
				qData << "Level\n1\n";
				Level = 1;
				lvlf = true;
				qData.close();
			}
		}
	}
	else
	{
        qData.close();
		ofstream qData("Data.txt", ios::app);
		qData << "Level\n1\n";
		Level = 1;
	    qData.close();
	}
}


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

class player
{
public:
	player();
	void Load();
protected:
	int Level;
	int Xp;
};

#endif 
You're not accessing the variables inside the class.

Create a function in the class, something like:
int getLevel();

Then:
1
2
int player::getLevel()
{ return level; }


And then, when you want to print it:
cout << player.getLevel();



You should also do:
1
2
3
4
5
6
int main()
{
    //code...

    return 0;
}
Last edited on
I had commands like this before, but I need to make Level and Xp different numbers and not to print them cuz I need to use them later in game (monsters will have HP like: player's HP*1,2 etc..)
But, by doing this, in player.cpp:
1
2
stringstream ss(qNewLevel);
ss >> Level;


You're saving the values to the protected variables, within the class.

And then, when you 'cout', in main():
cout << "Current Level: " << Level << endl << "Current XP: " << Xp << endl;

You're using cout to print the local variables created within main, and not the class. And you're more than likely getting warnings like the following, for using them.
warning C4700: uninitialized local variable 'level' used


To access the private variables within the class, you'll need member functions, like in my previous post.
Last edited on
and if I'll make Level and Xp public and not protected?
Just keep them protected and write member functions to access them.

And then, when you want to create monsters, do something like the following:
monsterHP = player.getHP() * 1.2;

If the players HP is 100, then all this is doing, is:
100 * 1.2 = 120
but after calling player.getHP() it will print player's HP on screen, or?

nvm, got it :) thanks!
Last edited on
Topic archived. No new replies allowed.