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 ;)
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;
}
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
elseif (choice == "Heal" || "heal"){
should be
elseif (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.
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.