Click here to Skip to main content
14,930,311 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How do I fix the < bool PlayerDied( PlayerData * pd ) > Function?

C++
#include <stdio.h>

int main(void) {
  printf("You will have life functions. You will get 3 lifes.\n");
char life [3];
int aa;
printf("You have 3 lifes");
printf("Press 'S' to start");
scanf("%i",&aa);

bool PlayerDied( PlayerData * pd )
{
    bool alive = false;
    --pd->Lives;       // player died - one less life
   return pd->Lives <= 0;
        alive = true;  // player still has a life left
    return alive;
}
  return 0;
}


What I have tried:

Hi This is for a text rpg I think
Posted
Updated 18-Mar-21 22:49pm
v3
Comments
Richard MacCutchan 19-Mar-21 5:55am
   
What is the problem that prevents you going and actually learning the language that you are trying to use?

Try, for instance:
C
#include <stdio.h>
#include <stdbool.h>

typedef struct
{
  // other fields here
  int lives;
} PlayerData;

bool player_died( PlayerData * pd );

int main(void)
{
  int iteration = 0;

  printf("You will have life functions. You will get 3 lifes.\n");

  PlayerData pd = {3};


  while ( ! player_died(&pd))
  {
    ++iteration;
    printf("iteration %d, the player is still alive\n", iteration);
  }
  printf("the player eventually died\n");

  return 0;
}

bool player_died( PlayerData * pd )
{
  if ( pd->lives )
  {
    --pd->lives;
    return false;
  }
  return true;
}
   
To add to what Rick says, C does not support nested functions - you cannot define your PlayerDied function inside the body of the main function.
And defining a function does not mean it gets executed - you have to call it in order to use it.

You need to learn the basics rather than copy'n'paste'n'guess: it'll be a lot quicker in the long run!
   
You could try reading what was written where you got that code :
Quote:

BTW - I don't think C has a boolean type so you can define one for yourself, along with true and false values.
You also need to define a PlayerData structure.

I will repeat myself, you need to define a PlayerData structure. If you are not ready to define and use data structures then you should not use that function and you should step back and learn the language more thoroughly. The alternative is to a use pile of global variables and that is a terrible idea.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900