Advertisement

if statements inside of if statements etc...

Started by March 25, 2001 11:53 AM
17 comments, last by Mccy257 23 years, 10 months ago
I am working on a text game- All I have so far is a battle, but it works great. However, inside the if statements below, if the player chooses to attack, a random number is picked from 1-6. If it''s 6, player misses, else hits. Now, do I continue the battle inside that same if statement or should I somehow start a new if statement at the very bottom. Sorry if this is confusing, just look at the code if you want. (DONT STEAL IT _________________________________________________________________ #include //includes #include #include #include void monster1(void); //prototype for first monster int player_hp; //global variables char player_name[20]; void main() { //seed randomizer printf("STTTTTTTTTTTTTTTTTTTTTTORRRRRRRRRY HERE!"); printf("\nEnter your name please:"); gets(player_name); printf("\nSir %s, you are about to fight a monster...\n",player_name); monster1(); /*there will be more here later */ } void monster1(void) { char input; int r; int enemy1_hp; enemy1_hp=30; player_hp=100; srand((unsigned)time(NULL)); r=rand()%6+1; system("cls"); //clear screen printf("Monster appeared!"); printf("\nWhat do you do?"); printf("\n\nA: Attack\nR: Run\n"); input=toupper(getche()); /******************************************************************************************/ //ATTACKING ENEMY if(input==''A'') { if(r==6) { printf("\nYou sword attack missed!"); printf("\nEnemy lunges at you and claws you in the face!"); player_hp-=10; printf("\n%s''s HP: %i\n",player_name,player_hp); } else { printf("\nSlice! Your attack worked!"); enemy1_hp-=10; printf("\nEnemy HP: %i\n",enemy1_hp); printf("%s''s HP: %i\n",player_name,player_hp); } } /*********************************************************************************************/ //THIS ELSE STATEMENT IS FOR RUNNING AWAY FROM BATTLE else { if(r==6) { printf("\nYou couldn''t run away!"); printf("\nEnemy takes a swing and nails you in the face!"); player_hp-=10; printf("\n%s''s HP: %i",player_name,player_hp); } else { printf("\nYou ran away and the monster was left behind!"); printf("\nPress any button to go fight another monster:"); } } }
Um... Dude...

Ever hear of a loop before?

  printf("Monster appeared!");printf("\nWhat do you do?");while (player_hp != 0) && (enemy1_hp !=0) && (escape != true)  { //Enter Battle Loop    printf("\n\nA: Attack\nR: Run\n");    input=toupper(getche());    if(input==''A'') //Attempt an Attack      {         if(r==6)          {            printf("\nYour sword attack missed!");            printf("\nEnemy lunges at you and claws you in the face!");            player_hp-=10;            printf("\n%s''s HP: %i\n",player_name,player_hp);          }        else          {            printf("\nSlice! Your attack worked!");            enemy1_hp-=10;            printf("\nEnemy HP: %i\n",enemy1_hp);            printf("%s''s HP: %i\n",player_name,player_hp);          }      }    else  // Attempt to Escape       {        if(r==6)          {            printf("\nYou couldn''t run away!");            printf("\nEnemy takes a swing and nails you in the face!");            player_hp-=10;            printf("\n%s''s HP: %i",player_name,player_hp);          }        else          {            printf("\nYou ran away and the monster was left behind!");            printf("\nPress any button to go fight another monster:");            escape = true;          }      }   } //Exit Battle Loop  


This will execute until either you or your enemy reach zero hit points, or if an escape attempt was successful. You''ll have to check outside the loop whether the player died, killed his enemy, or successfully escaped and handle each accordingly. But a simple conditional will take care of that. Hope this helped you out.
Advertisement
THANKS!
No problem, by the way, make sure escape is set to false before you enter the loop. Otherwise the loop won''t occur. I forgot to add that into my example above, sorry.
I''m getting some errors. It says: escape undeclared identifier and same thing for true. How do I declare these?
Here''s what I have: I get three errors- escape undeclared identifier, true undeclared identifie, ; missing before &&



#include
#include
#include

#define TRUE 1
#define FALSE !TRUE




int player_name; //global variable for the player''s name
int r;

void main()
{

int player_hp=100;
int enemy1_hp=30;

char input;

r=rand()%6+1;
srand((unsigned)time(NULL)); //seed randomizer



printf("\nEnter your name:");
gets(player_name);



printf("\nMonster appeared!");
printf("\nWhat do you do?");


while (player_hp != 0) && (enemy1_hp !=0) && (escape != TRUE)
{ //enter main battle loop

printf("\n\nA: Attack\tR: Run");
input=toupper(getch());
if(input==''A'') //Attempt an attack
{
if(r==6)
{
printf("\nYour sword attack missed!");
printf("\nEnemy lunges at you and claws you in the face!");
player_hp-=10;
printf("\n%s''s HP: %i\n",player_name,player_hp);

}

else
{
printf("\nSlice! Your attack worked!");
enemy1_hp-=10;
printf("\nEnemy HP: %i\n",enemy1_hp);
printf("%s''s HP: %i\n",player_name,player_hp);
}
}

else //Attempt to escape
{
if(r==6)
{
printf("\nYou couldn''t run away!");
printf("\nEnemy takes a swing and nails you in the face!");
player_hp-=10;
printf("\n%s''s HP: %i",player_name,player_hp);
}

else
{
printf("\nYou ran away and the monster was left behind!");
printf("\nPress any button to go fight another monster:");
escape = true;
}
}

} //Exit battle loop

Advertisement
try this:

Put these define statements at the top of your source file

#define true 1
#define false 0

declare this variable somewheres near the top too:

int escape=0;
Ciphersoftware website

Support the open source community

Those worked except I still get this nasty error:

; missing before &&
Um, C is case-sensitive meaning that ''TRUE'' and ''true'' aren''t the same. as for the && error, replace the current while loop with this:

while((player_hp != 0)&&(enemy1_hp !=0)&&(escape != true)){

whats wrong is that you need an extra set of brackets to enclose the entire statement

Justin Todd
http://justin-todd.tripod.com
Ciphersoftware website

Support the open source community

You need to declare escape as a boolean type and make sure to intialize it to false before you enter and loops that use it as a conditional part (like in my example).

To declare and initialize escape just do this:
  bool escape;escape = false;  


That should fix your compile errors.

This topic is closed to new replies.

Advertisement