monsterHP

Hi folk!

I recently started learning c++ and i love it! It's a great language.

However, i'm having a problem with this little game i wrote. Instead of running through one if statement, it runs through all of them. If you also have quicker ways to write a code like this i'd be very happy to learn ;)

Here's the output:
http://prntscr.com/2gu2nf

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
 // Main.CPP

#include <iostream>
#include <string.h>
#include <ctime>
#include <cmath>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;



int playerHP = 100;
int monsterHP = 100;

string choice;

int main(){





cout << "Attack - Heal" << endl;
do{

//
   srand (time(NULL));
int playerRand = rand() % 19 + 1;
int monsterRand = rand() % 30 + 1;
//

cin >> choice;


if (choice == "Attack" || "attack"){
    monsterHP = monsterHP - playerRand;
    cout << "Monster has " << monsterHP << " left!" << endl;
    cout << "You currently have " << playerHP << " left!" << endl;
}


if (choice == "Heal" || "heal"){
    playerHP = playerHP + playerRand;
    cout << "You healed yourself for " << playerRand << " !" << endl;
    cout << "You currently have " << playerHP << " left!" << endl;
}


}while(playerHP || monsterHP > 0);

}



I appreciate all help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(something)
{
//do this
}
else if(something different)
{
//do this
}
else if(something different)
{
//do this
}
else
{
//do this
}


You shouldn't seed your random numbers in a while loop btw. srand() belongs outside that loop and should just be called once.
Last edited on
1. Use an else statement as Mats suggested.
2. Here is a good link about if statements http://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm
3. You should only call srand() once in the program.
4. The condition in line 52 should use &&, otherwise the game would continue after the monster/player died.
5. You should indent all the lines of code in main().
example:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
	srand (time(NULL)); //randomize only ONCE
	int playerRand, //attack/heal values
	monsterRand;
	cout << "Attack - Heal" << endl;
	do{
		playerRand = rand() % 19 + 1;
		monsterRand = rand() % 30 + 1;

		cin >> choice;
       //rest of code
       return 0;
}


Hope this helps!
Last edited on
Thanks for your replies guys.

But unfortunately none of those have worked for me. My code is this currently:

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
// Main.CPP

#include <iostream>
#include <string.h>
#include <ctime>
#include <cmath>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;



int playerHP = 100;
int monsterHP = 100;

string choice;

int main(){


cout << "Attack - Heal" << endl;
srand (time(NULL));

do{

//

int playerRand = rand() % 19 + 1;
int monsterRand = rand() % 30 + 1;
//

cin >> choice;


if (choice == "Attack" || "attack"){
    monsterHP = monsterHP - playerRand;
    cout << "Monster has " << monsterHP << " HP left!" << endl;
    cout << "You currently have " << playerHP << " left!" << endl;
}
else if (choice == "Heal" || "heal"){
    playerHP = playerHP + playerRand;
    cout << "You healed yourself for " << playerRand << " !" << endl;
    cout << "You currently have " << playerHP << " left!" << endl;
}
else {
cout << "Error, try again." << endl;
}

}while(playerHP || monsterHP > 0);

cout << "Game over!" << endl;
}


I'll have to go now, but i'll favorite this page and read for more replies tomorrow and hopefully i'll get a solution. But thanks anyway! ;)
Last edited on
Computers don't think like humans - infact, they don't think at all.
That means you have to be explicit with all your intentions, because the computer wont know what you're thinking, and fill in the blanks for you.

if (choice == "Attack" || "attack"){

should be

if (choice == "Attack" || choice == "attack"){

and

else if (choice == "Heal" || "heal"){

should be

else if (choice == "Heal" || choice == "heal"){

Also, as heyyouyesyouiloveyou has said, the while loop condition on line 51 is wrong for several reasons.

First, you're not being explicit again. You're not comparing playerHP to any value explicitly, so the computer will assume you're trying to compare the truth-ness of that variable. If playerHP is a non-zero value, the condition will always be true.
Even if you compared playerHP to zero, it still wouldn't be right, because the loop would continue while either the player's health or the monster's health is more than zero.

}while(playerHP > 0 && monsterHP > 0);

Also, consider this: What if the user's choice is "AttACK" or any other permutation of lower-case and capitalized letters? I'm not saying that you need to handle these situations, but you need to decide if you want to treat these special cases with the else on line 48.

The easiest way of going about this would be to process the user's choice string in such a way that capitalized letters are changed into their lower-case counterparts. That way, you won't have to write out a special case for each permutation, and you can cut your conditions in half.
Last edited on
I think it might be an issue with your && and || logic. I don't think you are supposed to use them within a statement as you have.

For example, line 37 should read
else if(choice == "Heal" || choice == "heal")

The computer doesn't understand the if condition, so it just ignores the condition and executes the statement, which is the source of your problem.
oops, someone got to it before I did.

Also
1. you don't need a do-while loop. Since you initialize the player and monster health as 100, the condition will evaluate to true at the beginning.
Thanks a lot guys! My code works fine now. I learned a ton from this thread. :D
Topic archived. No new replies allowed.