Results 1 to 2 of 2

Thread: Why is health (HE && HE2) lower than 0?

  1. #1
    Join Date
    Mar 2017
    Posts
    1
    Platforms
    Windows

    Question Why is health (HE && HE2) lower than 0?

    why doesn't it stop at zero when I got a while loop for (HE > 0 || HE2 > 0)?

    Qt Code:
    1. #include <iostream>
    2. #include <ctime>
    3. #include <cstdlib>
    4. #include <unistd.h>
    5. using namespace std;
    6.  
    7. int randVal(int min,int max){
    8. return (rand() % (max - min + 1) + min);
    9. }
    10.  
    11. int main(){
    12. srand(time(NULL));
    13. int bb1,bb2,bb3,hh,aa,HE,ll,tp,na,at;
    14. int bb4,bb5,bb6,hh2,aa2,HE2,ll2,tp2,na2,at2;
    15.  
    16. string Mhead[] = {"( 0 ^ 0 )","| 0 * 0 |","( =^_^= )","( >_< ) ","(<@> <@>)"};
    17. string Mbody1[] = {"1#####1","[[[]]] "};
    18. string Mbody2[] = {"1#####1","[[[]]] "};
    19. string Mbody3[] = {"1#####1","[[[]]] ","| | "," _/|_ "};
    20. string Mname[] = {"Abra","Aipom","Amaura","Arbok","Archen","Aron","Bagon","Beedrill","Beldum","Bayleef","baltoy","Cacnea","Cascoon","Delphox","CHARIZARD",
    21. "Deino","Deerling","Ditto","Dragonair","Drampa","Drowzee","Dustox","Electrabuzz","Emboar","Fearow","Golem","Grimer","Guzzlord","Hypno","Inkay","Invysaur",
    22. "Jolteon","Jynx","Koffinf","Lapras","Latias","Latios","Charmander","Mew","Nidioking","Nidoqueen","Onix","Pichu","Pikachu"};
    23. string Mtype[] = {"Fire","Water","Earth","Air"};
    24.  
    25. HE = randVal(1,1000);
    26. ll = randVal(1,100);
    27. HE *= ll;
    28. tp = randVal(0,3);
    29. na = randVal(0,43);
    30. aa = randVal(0,500);
    31. at = randVal(0,1000);
    32.  
    33. cout<<"input 0,1,2,3, or 4 \n";
    34. cin>>hh;
    35. if(hh < 0 || hh > 4){
    36. cout<<"INVALID INPUT!! re-enter \n";
    37. cin>>hh;
    38. if(hh < 0 || hh > 4){
    39. cout<<"really...";
    40. sleep(3);
    41. return 0;
    42. }
    43. }
    44. cout<<"Now, input 1 or 0 \n";
    45. cin>>bb1;
    46. if(bb1 < 0 || bb1 > 1){
    47. cout<<"INVALID INPUT!! re-enter \n";
    48. cin>>bb1;
    49. if(bb1 < 0 || bb1 > 1){
    50. cout<<"really...";
    51. sleep(3);
    52. return 0;
    53. }
    54. }
    55. cout<<"again \n";
    56. cin>>bb2;
    57. if(bb2 < 0 || bb2 > 1){
    58. cout<<"INVALID INPUT!! re-enter \n";
    59. cin>>bb2;
    60. if(bb2 < 0 || bb2 > 1){
    61. cout<<"really...";
    62. sleep(3);
    63. return 0;
    64. }
    65. }
    66. cout<<"once more, BUT this time 0,1,2, or 3! \n";
    67. cin>>bb3;
    68. if(bb3 < 0 || bb3 > 3){
    69. cout<<"INVALID INPUT!! re-enter \n";
    70. cin>>bb3;
    71. if(bb3 < 0 || bb3 > 3){
    72. cout<<"really...";
    73. sleep(3);
    74. return 0;
    75. }
    76. }
    77. cout<<"-------------------- \n";
    78. cout<<"YOUR MONSTER \n \n";
    79.  
    80. cout<<Mhead[hh]<<endl;
    81. cout<<Mbody1[bb1]<<endl<<Mbody2[bb2]<<endl<<Mbody3[bb3]<<endl<<endl;
    82.  
    83. cout<<"Name: "<<Mname[na]<<endl;
    84. cout<<"Level: "<<ll<<endl;
    85. cout<<"Type: "<<Mtype[tp]<<endl;
    86. cout<<"Health: "<<HE<<endl;
    87. cout<<"Armour: "<<aa<<endl;
    88. cout<<"Attack: "<<at<<endl;
    89.  
    90. sleep(4);
    91.  
    92. cout<<"-------------------- \n";
    93. cout<<"HOSTILE MONSTER \n \n";
    94.  
    95. HE2 = randVal(1,1000);
    96. ll2 = randVal(1,100);
    97. HE2 *= ll2;
    98. tp2 = randVal(0,3);
    99. na2 = randVal(0,43);
    100. aa2 = randVal(0,500);
    101. at2 = randVal(0,1000);
    102. bb4 = randVal(0,1);
    103. bb5 = randVal(0,1);
    104. bb6 = randVal(0,2);
    105. hh2 = randVal(0,1);
    106.  
    107. cout<<Mhead[hh2]<<endl;
    108. cout<<Mbody1[bb4]<<endl<<Mbody2[bb5]<<endl<<Mbody3[bb6]<<endl<<endl;
    109.  
    110. cout<<"Name: "<<Mname[na2]<<endl;
    111. cout<<"Level: "<<ll2<<endl;
    112. cout<<"Type: "<<Mtype[tp2]<<endl;
    113. cout<<"Health: "<<HE2<<endl;
    114. cout<<"Armour: "<<aa2<<endl;
    115. cout<<"Attack: "<<at2<<endl;
    116.  
    117. sleep(4);
    118.  
    119. cout<<"-------------------- \n";
    120. cout<<" !!!FIGHT!!! \n";
    121. cout<<Mhead[hh]<<" "<<Mhead[hh2]<<endl;
    122. cout<<Mbody1[bb1]<<" "<<Mbody1[bb4]<<endl;
    123. cout<<Mbody2[bb2]<<" vs "<<Mbody2[bb5]<<endl;
    124. cout<<Mbody3[bb3]<<" "<<Mbody3[bb6]<<endl;
    125.  
    126. HE += aa;
    127. HE2 += aa2;
    128. ll /= 2;
    129. ll2 /= 2;
    130. HE += aa;
    131. HE2 += aa2;
    132.  
    133. while(HE > 0 || HE2 > 0){
    134.  
    135. if(tp == tp2){
    136. HE -= at2;
    137. HE2 -= at;
    138. cout<<"You attacked for "<<at<<endl;
    139. cout<<"Their health is "<<HE2<<endl;
    140. cout<<"They attacked for "<<at<<endl;
    141. cout<<"Your health is "<<HE<<endl;
    142. sleep(2);
    143. }
    144. if(tp == 1 && tp2 == 2){
    145. HE -= at2;
    146. HE2 -= at;
    147. cout<<"You attacked for "<<at<<endl;
    148. cout<<"Their health is "<<HE2<<endl;
    149. cout<<"They attacked for "<<at<<endl;
    150. cout<<"Your health is "<<HE<<endl;
    151. sleep(2);
    152. }
    153. if(tp == 1 && tp2 == 3){
    154. at2 *= 1.2;
    155. HE -= at2;
    156. HE2 -= at;
    157. cout<<"You attacked for "<<at<<endl;
    158. cout<<"Their health is "<<HE2<<endl;
    159. cout<<"They attacked for "<<at<<endl;
    160. cout<<"Your health is "<<HE<<endl;
    161. sleep(2);
    162. }
    163. if(tp == 1 && tp2 == 0){
    164. at *= 1.2;
    165. HE -= at2;
    166. HE2 -= at;
    167. cout<<"You attacked for "<<at<<endl;
    168. cout<<"Their health is "<<HE2<<endl;
    169. cout<<"They attacked for "<<at<<endl;
    170. cout<<"Your health is "<<HE<<endl;
    171. sleep(2);
    172. }
    173. if(tp == 0 && tp2 == 1){
    174. at2 *= 1.2;
    175. HE -= at2;
    176. HE2 -= at;
    177. cout<<"You attacked for "<<at<<endl;
    178. cout<<"Their health is "<<HE2<<endl;
    179. cout<<"They attacked for "<<at<<endl;
    180. cout<<"Your health is "<<HE<<endl;
    181. sleep(2);
    182. }
    183. if(tp == 0 && tp2 == 2){
    184. at *= 1.2;
    185. HE -= at2;
    186. HE2 -= at;
    187. cout<<"You attacked for "<<at<<endl;
    188. cout<<"Their health is "<<HE2<<endl;
    189. cout<<"They attacked for "<<at<<endl;
    190. cout<<"Your health is "<<HE<<endl;
    191. sleep(2);
    192. }
    193. if(tp == 0 && tp2 == 3){
    194. HE -= at2;
    195. HE2 -= at;
    196. cout<<"You attacked for "<<at<<endl;
    197. cout<<"Their health is "<<HE2<<endl;
    198. cout<<"They attacked for "<<at<<endl;
    199. cout<<"Your health is "<<HE<<endl;
    200. sleep(2);
    201. }
    202. if(tp == 2 && tp2 == 0){
    203. at2 *= 1.2;
    204. HE -= at2;
    205. HE2 -= at;
    206. cout<<"You attacked for "<<at<<endl;
    207. cout<<"Their health is "<<HE2<<endl;
    208. cout<<"They attacked for "<<at<<endl;
    209. cout<<"Your health is "<<HE<<endl;
    210. sleep(2);
    211. }
    212. if(tp == 2 && tp2 == 1){
    213. at2 *= 1.2;
    214. HE -= at2;
    215. HE2 -= at;
    216. cout<<"You attacked for "<<at<<endl;
    217. cout<<"Their health is "<<HE2<<endl;
    218. cout<<"They attacked for "<<at<<endl;
    219. cout<<"Your health is "<<HE<<endl;
    220. sleep(2);
    221. }
    222. if(tp == 2 && tp2 == 3){
    223. at *= 1.2;
    224. HE -= at2;
    225. HE2 -= at;
    226. cout<<"You attacked for "<<at<<endl;
    227. cout<<"Their health is "<<HE2<<endl;
    228. cout<<"They attacked for "<<at<<endl;
    229. cout<<"Your health is "<<HE<<endl;
    230. sleep(2);
    231. }
    232. if(tp == 3 && tp2 == 0){
    233. HE -= at2;
    234. HE2 -= at;
    235. cout<<"You attacked for "<<at<<endl;
    236. cout<<"Their health is "<<HE2<<endl;
    237. cout<<"They attacked for "<<at<<endl;
    238. cout<<"Your health is "<<HE<<endl;
    239. sleep(2);
    240. }
    241. if(tp == 3 && tp2 == 1){
    242. at *= 1.2;
    243. HE -= at2;
    244. HE2 -= at;
    245. cout<<"You attacked for "<<at<<endl;
    246. cout<<"Their health is "<<HE2<<endl;
    247. cout<<"They attacked for "<<at<<endl;
    248. cout<<"Your health is "<<HE<<endl;
    249. sleep(2);
    250. }
    251. if(tp == 3 && tp2 == 2){
    252. at2 *= 1.2;
    253. HE -= at2;
    254. HE2 -= at;
    255. cout<<"You attacked for "<<at<<endl;
    256. cout<<"Their health is "<<HE2<<endl;
    257. cout<<"They attacked for "<<at<<endl;
    258. cout<<"Your health is "<<HE<<endl;
    259. sleep(2);
    260. }
    261. }
    262. cout<<endl;
    263. if(HE <= 0){
    264. cout<<"YOUR MONSTER LOST \n";
    265. cout<<"Their health is "<<HE2<<endl;
    266. cout<<"Your health is "<<HE<<endl;
    267. }
    268. if(HE2 <= 0){
    269. cout<<"YOUR MONSTER WON!!!! \n";
    270. cout<<"Your health is "<<HE<<endl;
    271. cout<<"Their health is "<<HE2<<endl;
    272. }
    273.  
    274. string endGAME;
    275. cout<<"type end to quit \n";
    276. cin>>endGAME;
    277.  
    278. return 0;
    279. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 19th April 2017 at 11:40. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why is health (HE && HE2) lower than 0?

    Shouldn't it be "while(HE > 0 && HE2 > 0)"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt Creator UI compiler 4.8.5 includes my classes all lower case
    By Markus in forum Qt Tools
    Replies: 2
    Last Post: 9th October 2013, 17:39
  2. Scrollbar for lower resolution ...
    By RJ in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2013, 15:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.