I'm programming this RPG, and I can't get past the first battle. I can't figure out how to get the computer to break the loop when then enemy's health (enhp) is less than 1. I tried using an if statement, but then it didn't loop at all. Could you guys help me out?

#include <iostream>
#include <string>
using namespace std;
int main (void)
{
int num, random_integer, hp= 100, enhp= 50;
while (hp >=1 or enhp >=1)
{
{srand((unsigned)time(0));
for(int index=0; index<20; index++)
{random_integer = (rand()%10)+1;
hp = hp-random_integer;
if (hp>=1)
{cout<<"The enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health.";
cout<<"\n1)Attack!\n 2)I've had enough- run away!";
cin>>num;}
if(num ==1)
{srand((unsigned)time(0));
for(int index=0; index<20; index++)
{random_integer = (rand()%10)+1;}
enhp = enhp-random_integer;
cout<<"You have done "<<random_integer<<" damage." ;}
else
cout<<"You have fled";
}
system("PAUSE");
return 0;
}
}
}
//...
if (enhp < 1)
    break;

enhp = enhp-random_integer;
//...

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.)

thanks, your posts have been very helpful. Unfortunately, I've noticed another problem. My else that lets the player exit the battle only works in the initialization. Do you know why that might be the case?

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]

#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;
}

This code won't work. All I did was copy and paste it from the larger file, and it said that it expected a declaration before the curly brace on line 30.

You're still calling srand() more than once.

Also try indenting your code and you might see your problem! Or at least stand a chance, thats too painful to try and read tbh.

Chris

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.

I have made the suggested changes in code, and now the only error that shows up is "expected primary expression before else on the final else." Additionally I apologize for my cluttered code, I sometimes forget that I'm the only one on earth who likes reading it.

{if(num ==1)
      
      {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;
      }
      else
      cout<<"You have done "<<random_integer<<" damage." ;
      }
      else
      {
      cout<<"You have fled"; 
      break;
      }

Your last else has no if to go with it.....

Chris

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

you cant go else else, im not sure but shouldn't it be something like

if (enhp < 1) //Victory condition. Breaks loop after enemy death
{
cout<<"The masked man falls to the ground, defeated.";
break;
}
else if (enhp !< 1)
{ 
cout<<"You have done "<<random_integer<<" damage." ;
}
else
{
cout<<"You have fled";
break;
}

and then i dont know what you would do about thre last else, you need a way to determine wether or not to flee.

EDIT: im sort of nub, so im not entirely sure about this

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.

^^i know, hence the

then i dont know what you would do about thre last else, you need a way to determine wether or not to flee.

but nice to know i'm not entirely wrong. #0: whatcha next step? ;-)

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.

I'm sorry I haven't replied for some time, I've been very busy recently. Anyway, I've edited the code, spaced it out a little, and now I only get four errors. The compiler expects a primary expression and a semicolon before the elses at lines 36 and 40.

#include <iostream>
#include<time.h>
#include <string>
using namespace std;
int main (void)
{
int array[2], random_integer, hp= 100, enhp= 50;
{srand((unsigned)time(0));

while (hp >=1 && enhp >=1)
{
for(int index=5; index<10; index++)
{random_integer = (rand()%10)+1;
hp = hp-random_integer;
if (hp>=1) 
{
           cout<<"\nThe enemy does "<<random_integer<<" damage, leaving you with "<<hp<<" health.";
           cout<<"\n1)Attack!\n 2)I've had enough- run away!\n";
       cin>>array[0];}
else
              {cout<<"You have died, better luck next time,";
system("Pause");
return 0;}
       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");}
return 0;
}

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.