I didn't post all the code because it's quite big and I don't want to occupy your time.
That's the code you will propably need:
Code:
void aventura(Personatge *personatge, Enemic *enemics, FILE *h, int nEnemics){
Cua cua;
int nMort = 0, nExpguany = 0, nRivals = 0;
printf("%d\n",nEnemics);
cua = CUA_crea();
mostraEnemics(enemics,nEnemics,&cua,&nRivals); /
lluita(personatge,&cua,&nMort,&nExpguany,enemics);//That's the function where I have the problem.
printf("LUCHA FINALIZADA\n");
levelup(personatge,nMort,nExpguany,h);
printf("LEVELUP FINALIZADO\n");
CUA_destrueix(&cua);
}
Function LLUITA
Code:
void lluita(Personatge *personatge, Cua *cua, int *nMort, int *nExpguany,Enemic *enemics){
Enemic aux;
int nDaus = 0, nFinal = 0, nVidamax = 0;
if(cua->cap == cua->ult){
printf("Error, cua buida!\n");
}
else{
nVidamax = personatge->nVida;
while((*cua).cap != (*cua).ult && *nMort != 1){
aux = CUA_primer(*cua);
CUA_desencua(&(*cua));
personatge->nVida = nVidamax;
nFinal = 0;
printf("Combat entre %s i %s\n", personatge->strNom, aux.strNom);
printf("Prem una tecla per continuar...\n");
getchar();
fflush(stdout);
while(nFinal == 0){
atacant(personatge, &aux, &nMort, nDaus, &nFinal); //HERE IS THE FUNCTION WHERE THE PROBLEM APPEARS
if(nFinal == 0){
enemic(personatge, &aux, &nMort, nDaus, &nFinal);
}
if(nFinal == 1 ){
printf("ENTRA EN IF nFinal = 1\n");
if( *nMort == 0){
printf("BIEN, SE ACTUALIZA LA EXPERIENCIA\n");
*nExpguany = *nExpguany + enemics->nExperiencia;
printf("%d\n",*nExpguany);
printf("Final del combat!\n");
}
else{
printf("NO EXP!!\n");
printf("Final del combat!\n");
}
printf("Prem una tecla per continuar...\n");
getchar();
fflush(stdout);
}
}
}
}
}
ATACANT
Code:
void atacant(Personatge *personatge, Enemic *aux, int **nMort, int nDaus, int *nFinal){ //nFinal means that the enemy is dead.
int nMal = 0;
nDaus = rand() %21; //Tira daus
if (nDaus == 1){ //Fail
printf("%s ataca i... (%d) falla!\n",personatge->strNom, nDaus);
fflush(stdout);
printf("Vida de %s\n",aux->strNom);
printf("%d\n",aux->nVida);
fflush(stdout);
}
if(nDaus >= 2 && nDaus <= 19){ //Normal
printf("%s ataca i... (%d) Atac normal.\n",personatge->strNom, nDaus);
nMal = personatge->nAtac - aux->nDefensa;
if(nMal < 0){
nMal = 0;
}
fflush(stdout);
printf("Fa %d de mal a %s\n", nMal, aux->strNom);
aux->nVida = aux->nVida - nMal;
if(aux->nVida <= 0){
printf("%s ha mort!\n", aux->strNom); //HERE THE %S IS PRINTED BUT UNDER THE REST OF THE TEXT: "ha mort!\n"
*nFinal = 1;
}
else{
printf("Vida de %s\n",aux->strNom);
printf("%d\n",aux->nVida);
}
}
if(nDaus == 20){ //Critical -> HERE APPEARS A CORE DUMPED!
printf("%s ataca i... (%d) Atac critic!\n", personatge->strNom, nDaus);
nMal = (personatge->nAtac *2) - aux->nDefensa;
if(nMal < 0){
nMal = 0;
}
fflush(stdout);
printf("Fa %d de mal a %s\n", nMal, aux->strNom);
aux->nVida = aux->nVida - nMal;
if(aux->nVida <= 0){
printf("%s ha mort!\n");
*nFinal = 1;
}
else{
printf("Vida de %s es: %d\n", aux->strNom, aux->nVida);
}
}
}
The critical function is the same that the normal function, but a CORE DUMPED appears.... (segmentation fault).