[console] 0+0 = NULL not 0?

hi, I have little problem, title says all, here is code:

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
85
86
87
88
89
90
91
92
93
94
95
96
void player::CreateItem()
{
	int lines = 0;
	bool ItemFound = false;
	string line;
	string save[100];
	string Item;
	fstream getlines("Data.txt");
	while(!getlines.eof())
	{
		getline(getlines, line);
		lines++;
	}
	cout << "lines" << endl;
	getlines.close();
	lines = lines - 2;
	fstream Data("Data.txt");
	for(int i=0;i<=lines;i++)
	{
		getline(Data, save[i]);
	}
	Data.close();
	int s;
	for(s=0;s<=lines;s++)
	{
		cout << "finding item" << endl;
		if(save[s] == "Item")
		{
			cout << "Item found" << endl;
			s = s + 1;
			stringstream hp(save[s]);
			hp >> sHpRunes; // after cout sHpRunes is 0 and player::GetHpRunes() is 0 too
			save[s] = sHpRunes + player::GetHpRunes(); // now after cout in txt file I have "NULL" and in console *nothing*
			s = s + 1;
			stringstream power(save[s]);
			power >> sPowerRunes;
			save[s] = sPowerRunes + player::GetPowerRunes();
			ItemFound = true;
		}
	}
	cout << "rewrite" << endl;
	ofstream rew("Data.txt");
	for(int o=0;o<=lines;o++)
	{
		rew << save[o] << endl;
	}
	if(ItemFound == false)
	{
		cout << "Item not found" << endl;
		ofstream InsertItem("Data.txt", ios::app);
		InsertItem << "Item" << endl << player::GetHpRunes() << endl << player::GetPowerRunes() << endl;
	}
}

int player::GetHpRunes()
{
	int HpRunes = 0;
	bool HpRuneFound = false;
	string HpRune;
	fstream FindHpRune("Data.txt");
	while(!FindHpRune.eof())
	{
		getline(FindHpRune, HpRune);
		if(HpRune == "Runes")
		{
			getline(FindHpRune, HpRune);
			stringstream ss(HpRune);
			ss >> HpRunes;
			HpRuneFound = true;
		}
	}
	FindHpRune.close();
	return HpRunes;
}

int player::GetPowerRunes()
{
	int PowerRunes = 0;
	bool PowerRuneFound = false;
	string PowerRune;
	fstream FindPowerRune("Data.txt");
	while(!FindPowerRune.eof())
	{
		getline(FindPowerRune, PowerRune);
		if(PowerRune == "Runes")
		{
			getline(FindPowerRune, PowerRune);
			getline(FindPowerRune, PowerRune);
			stringstream ss(PowerRune);
			ss >> PowerRunes;
			PowerRuneFound = true;
		}
	}
	FindPowerRune.close();
	return PowerRunes;
}
You're assigning integers to strings. That won't work (not the way you want it). You need a stringsteam to convert string to int as well as int to string. Use the stringstream's str() method.
1
2
stringstream hp(save[s]);
			hp >> sHpRunes; // after cout sHpRunes is 0 and player::GetHpRunes() is 0 too 
What's your point?
to find Runes and change item stats in txt file, I can send you whole code if you want...
That's not what I meant. Why did you post those lines?
I'm doubt it is a good idea to hold your integers in strings (except when you print them), but let's ignore that for now.
Let's say you have a string and want to add 1 to it.
1
2
3
4
5
6
7
8
std::string str = "1";//this isn't smart at all...
std::stringstream s1(str);
int val;
s1 >> val;
val += 1;
std::stringstream s2;//you can (should) reuse s1
s2 << val;
str = s2.str();

edit: typo
Last edited on
but...

I'm using strings because I'm getting words, integers and sentences from data.txt, so this is easiest way to do it, then I wanna change string which I've got from data.txt (0) to int and then get HpRunes from file (0 too) and do 0+0 (later you will have more runes so it will be 0+10, then 10 + Runes etc, but I'm getting NULL everytime, even if I have 10 runes... (it should be 10)
Do as you like. In case you didn't notice, the code I posted uses your logic (and works). I'm sure you'll be able to use it in your program..
ahh, thanks, I really didn't noticed that :P
Topic archived. No new replies allowed.