While Loop, Wont love me.

sorry for the dumb question. but after days of thinking and research, I had no avail. While loop hates me. if someone can provide me answers or maybe just a tip on what's wrong then, please... What your looking at is a crappy homemade RPG battle simulator. its suppose to exit and save data to the respective character file. but the loop keeps going even when the hp reaches the -1.
I was working on this before I knew about functions and projects so excuses the messy work...

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  #include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main (){
	//varibles
	string Namem,Name,ability,pathm, playerp,savep,loadm,loadp,loads;
	int expm,hm,am,dm,ab,hp,ap,dp,lvl,exp, expn, left, ph, i=1;
	const int A=1,D=2,M=3;
	ifstream mons,player;
	ofstream plays;
	char choice, attack, defend, choice1, atkm,defm, a,b,c,d,e;
	pathm="C:/Users/jairy/Documents/Code/C Dungeon/charaters/mons/";
	playerp="C:/Users/jairy/Documents/Code/C Dungeon/charaters/Players/";
	savep="C:/Users/jairy/Documents/Code/C Dungeon/charaters/Players/";
	// loading charater
	cout << "Welcome to the battle simulator Dev mode version 1.0" << endl; 
	cout << "Please load charater name:";
	cin >> Name;
	do {
		loadp=Name+".txt";
		playerp=playerp+loadp;
		player.open(playerp.c_str());
		if (player.is_open()){
			player>>Name;
            player>>hp;
            player>>ph;
		    player>>ap;
			player>>dp;
			player>>lvl;
			player>>exp;
			player>>expn;
			player.close();
	}
	else {
		cout << "Failed to load charater, Try again";
		system ("pause");
		a='r';
	}
	}while (a=='r');
	cout << "File lodded, Now please load the desired monster:";
	cin >> Namem;
		do {
		loadm=Namem+".txt";
		pathm=pathm+loadm;
		mons.open(pathm.c_str());
		if (mons.is_open()){
			mons >> Namem;
			mons >> ability;
			mons >> hm;
			mons >> am;
			mons >> dm;
			mons >> expm;
			mons >> ab;
			mons.close();
	}
	else {
		cout << "Failed to load charater, Try again";
		system ("pause");
		b='r';
	}
	}while (b=='r');
	cout << "File loaded, continiue to battle sequence" << endl;
	// Battle seqeunce
	system("cls");
	cout << Namem << " Appereard before you" << endl;
	cout << endl;
	system ("Pause");
	system("cls");
	while (0<hp ||  0<hm ){
		cout << "Turn:" << i << endl;
		cout << endl;
		cout << endl;
		cout << Namem << " hp:" << hm << endl;
		cout << endl;
		cout << Name << " Hp:" <<  hp << endl;
		cout << "How will you react (a-Attack,b-Defend):";
		cin  >> choice;
		
		
	      if (choice=='a'){
		  
			cout << "You swinged your sword." <<endl;
			hm=hm-ap;
			cout << Namem << " Hp is now " << hm <<endl;
			cout << Namem << " Attacked back" << endl;
				hp=hp-am;
				cout << "It hit! leaving you with " << hp << " Health" << endl;	
			}
		if (choice=='b'){		
			cout << "You held your sheild close..." << endl;
			cout << Namem << " Attacked back" << endl;
			 hp=hp-(am-dp);
				cout << "It hit! leaving you with " << hp << " Health" << endl;	
		}
	
	    
	
			system ("pause");
			
		system ("cls");
		cout << Name<<" Hp:" << hp << " " << Namem << "'s Hp:" << hm << endl;
		i++;
		system("pause");
		system ("cls");		
			cin.clear();
	}  
	// Saving and file data
cout << "Saving... " << endl;
	loads=Name +".txt";
	savep=savep+loads;
	plays.open(savep.c_str());
	if (hm<=0) {
		cout << "You Won, (Happy computer doot)" << endl;
		cout << "Adding exp..." << endl;
		exp=exp+expm;
		plays << Name << endl;
		if (exp>expn){
			lvl++;
			exp=0;
			expn=expn+50;
			cout << "Level up! you gained a level, stats are raised." << endl;
			system("pause");
			hp=hp+50;
			ph=ph+50;
			ap=ap+7;
			dp=dp+4;
			system("cls");
		}
		else{
		left=expn-exp;
		cout << "Logging exp, you have " << left << "exp left before level up" << endl;
		}
		cout << "Logging your new data" << endl;
		plays<< Name << endl;
		plays<< hp <<endl;
		plays<<ph <<endl;
		plays<<am <<endl;
		plays<<dm <<endl;
		plays<<lvl <<endl;
		plays<<exp <<endl;
		plays<<expn <<endl;
		system ("cls");
	return 0;
	}
if (hp<=0) {
	cout << "You died" << endl;
	system ("Pause");
	plays<< hp << endl;
	plays<<ph << endl;
	plays<<am << endl;
	plays<<dm << endl;
	plays<<lvl << endl;
	plays<<exp << endl;
	plays<<expn << endl;
	system ("cls");
	cout << "This charater is no longer usable... Create a new one, embark again" << endl;
	plays.close();
	
return 0;	
	
}

	
	
	
	
}


If you want to try this out and see what Im saying, change the paths to your respective path and make 2 files a human and a monster
looks like this:
human.txt | monster.txt
name | name
hp | ability
hp | hp
atk | atk
def | def
lvl | ability(int)
exp | exp
expn |
Last edited on
but the loop keeps going even when the hp reaches the -1.
You wrote
 
while (0<hp ||  0<hm ){
so the loop will continue even if hp is less than or equal to zero, as long as hm is greater than zero. Maybe you meant
 
while (0<hp &&  0<hm ){
but doesn't that mean both have to reach 0? in the loop only 1 should reach 0, cause there can only be one victor...
No, it means that the while loop runs while hp>0 and hm>0. So BOTH of them must be positive to continue the loop. That means that it will exit the loop if hp<=0 or hm<= 0 or both are <= 0.
Man, do I look stupid...
Nah, this is a common mistake. Everybody's done it, you're fine.
Last edited on
Topic archived. No new replies allowed.