Did you mean to loop until both were dead, or until one was dead?
while (hp >=1 or enhp >=1)
Reads (in english) "While the player health is at least one or the enemy health is at least one, keep fighting"
I think you wanted to stop if either one died:
while (hp >=1 and enhp >=1)
Reads (in english) "While the player health is at least one AND the enemy health is at least one, keep fighting"
So this loop will stop when either one is dead.
(But the break would work too.)
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
Comments on the original code (that's all I have to look at):
You're really only supposed to call srand() once per run you don't have to call it everytime you want a random number.
In the original code, the 'I want to quit' option prints "You have fled" but it is still inside the for loop so it keeps fighting.
The outer while loop that we spent the time talking about is actually never tested. When the for loop terminates, the main() returns before the while gets a chance to be tested.
If your code has changed significantly, please re-post it.
Please use language specific code tags:
[code=c++]
//your code here
[/code]
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
Ok...I will do this once, feel free to re-format afterwards, but here is your code. I put each brace '{' or '}' on its own line and then begged my editor to try to make it look better.
(I made no other changes, even though it needed them.)
#include <iostream>
#include <string>
using namespace std;
int main (void)
{
int num, random_integer, hp= 100, enhp= 50;
while (hp >=1 and enhp >=1)
{
srand((unsigned)time(0));
for(int index=5; index<10; index++)
{
random_integer = (rand()%10)+1;
hp = hp-random_integer;
if (hp>=1) //remember, multiline if statements require brackets to function
{
cout<<"\nThe enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health.";
cout<<"\n1)Attack!\n 2)I've had enough- run away!";
cin>>num;
}
else
{
cout<<"You have died, better luck next time,";
system("Pause");
return 0;
}
{
if(num ==1)
{
srand((unsigned)time(0));
for(int index=10; index<20; index++)
{
random_integer = (rand()%10)+1;
}
enhp = enhp-random_integer;
{
if(enhp< 1) //Victory condition. Breaks loop after enemy death
{
cout<<"The masked man falls to the ground, defeated.";
break;
}
}
cout<<"You have done "<<random_integer<<" damage." ;
}
else
{
cout<<"You have fled"; break;
}
}
}
}
}
}
cout<<"Terrified, you run through the streets. Where is you father? If anybody'll know what to do, it'll be him.";
system("PAUSE");
return 0;
}
As noted by the previous poster, you're still calling srand() from multiple locations and from within a loop.
I would put one call to srand() between line 6 and line 7 and then get rid of the rest.
Line 7 should read while (hp >=1 && enhp >=1)
You don't need the for loop that starts on line 10 above, the while loop on line 7 will keep you looping until someone is dead.
If you get rid of the srand() on line 29, you can replace the for loop on lines 30-33 with the one call on line 32.
You appear to have too many closing braces '}'
Try that and see what you have.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
What is the '{' on line 1 before the if for?
Where is the '{' after the if on line 1?
Why do you have '{' '}' around the statement on line 3?
What is the '{' on line 5 for?
Line 14 appears to be the else for the if on line 1. The else needs to follow either, the single statement that follows the if, or the block of statements surrounded by '{' '}'. Neither is true so the compiler complains.
(The compiler has no clue which if statement line 14 might belong to, but it does know that it is not following a statement or block that was the 'true part' of an if.)
So, recommended changes:
remove the leading '{' on line 1 (unless it serves a purpose later)
put a '{' on line 2
remove the '{' '}' surrounding line 3
remove the '{' on line 5
add a '}' (preferably on its own line) between lines 13 and 14
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
Bladtman242
You're right, you can't go else ... else. Your second test however (if written right as else if (! enhp < 1) or as else if (enhp >= 1) ) is the true inverse of the first condition and the last else would never be executed.
The first else was fine as written. The second else was supposed to be the else for if (num == 1)
In the full code, the user is prompted to select 1 for attack again, or 2 to flee.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
I'm kinda waiting on the OP (original poster) to see if they have been able to make the changes and get it to work so they can mark the thread solved or if there is more work to do to get it functional.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
That's still pretty painful to look at, maybe if your code tag worked (use [code=c++] to start it (no spaces in the tag).
You have a for loop that i think was supposed to be closed, but was not:
for(int index=5; index<10; index++)
{random_integer = (rand()%10)+1;
Did you mean to put a closing '}' on that second line?
Taking a section of code, (with the problems in it) ... putting each brace on its own line and eliminating extra braces and adding any missing braces.
if (array[0]==1)
{
for(int index=7; index<12; index++)
{
random_integer = (rand()%10)+1;
}
enhp = enhp-random_integer;
if(enhp< 1)
{
cout<<"Victory";
break;
}
else
{
cout<<"You have done "<<random_integer<<" damage.\n";
}
}
else if (array[0]==2)
{
cout<<"You have fled";
break;
break;
system("pause");
return 0;
}
else
{
cout<<"Invalid response, please choose again.";
system("pause");
}
Is that closer to what you want?
Does that compile?
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116