What this program does is to receive as input an integer number that will be the amount of monsters that will be created, after that it will receive as input the information of the n monsters and then it will receive another input that will be a "search" that will look for the monsters that are of a certain Type and that have CR between the two values placed. For example I put as input:
6
Bugbear Humanoid Medium 16 27 1
Hobgoblin Humanoid Medium 18 11 0.5
Cult_Fanatic Humanoid Medium 13 33 2
Zombie Undead Medium 8 22 0.25
Imp Fiend Tiny 13 10 1
Air_Elemental Elemental Large 15 90 5
Humanoid 0 1
Then it will look for monsters of type humanoid and that have CR between 0 and 1 and the output should be:
Bugbear - Medium Humanoid, AC:16, HP:27, CR:1.00
Hobgoblin - Medium Humanoid, AC:18, HP:11, CR:0.50
The problem is that when doing this my program gives:
Bugbear - Medium Humanoid, AC:16, HP:27, CR:1.00
Hobgoblin - Medium Humanoid, AC:18, HP:11, CR:0.50
Cult_Fanatic - Medium Humanoid, AC:13, HP:33, CR:2.00
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <math.h>
#include "our_ints.h"
#include "our_doubles.h"
const char *author ="Alexandre Santos";
typedef struct {
char nome[100];
char tipo[100];
char tamanho[100];
int ac;
int hp;
double cr;
} Monstro;
Monstro criar_monstro(char *nome, char *tipo, char *tamanho, int ac, int hp, double cr) {
Monstro novo_monstro;
strcpy(novo_monstro.nome, nome);
strcpy(novo_monstro.tipo, tipo);
strcpy(novo_monstro.tamanho, tamanho);
novo_monstro.ac = ac;
novo_monstro.hp = hp;
novo_monstro.cr = cr;
return novo_monstro;
}
void escrever_monstro(Monstro *monstro) {
printf("%s - %s %s, AC:%d, HP:%d, CR:%.2f\n", monstro->nome, monstro->tamanho, monstro->tipo, monstro->ac, monstro->hp, monstro->cr);
}
void monstros_to_monstros_ptr1(Monstro *in, Monstro **out, int n) {
for (int i = 0; i < n; i++) {
out[i] = &in[i];
}
}
int pesquisa_monstros_tipo(Monstro **in, int n, Monstro **out, char *tipo) {
int count = 0;
for (int i = 0; i < n; i++) {
if (strcmp(in[i]->tipo, tipo) == 0) {
out[count] = in[i];
count++;
}
}
return count;
}
int pesquisa_monstros_cr(Monstro **in, int n, Monstro **out, double cr_min, double cr_max) {
int count = 0;
for (int i = 0; i < n; i++) {
if (in[i]->cr > cr_min && in[i]->cr < cr_max) {
out[count] = in[i];
count++;
}
}
return count;
}
void testF() {
int n_monstros; // número de monstros
scanf("%d", &n_monstros);
// array de monstros
Monstro monstros[n_monstros];
// ler os dados de entrada e criar os monstros usando o construtor
char nome_monstro[100];
char tipo_monstro[100];
char tamanho_monstro[100];
int ac;
int hp;
double cr;
for (int i = 0; i < n_monstros; i++) {
scanf("%s %s %s %d %d %lf", nome_monstro, tipo_monstro, tamanho_monstro, &ac, &hp, &cr);
monstros[i] = criar_monstro(nome_monstro, tipo_monstro, tamanho_monstro, ac, hp, cr);
}
// criar array de ponteiros para Monstro
Monstro *monstros_ptr[n_monstros];
monstros_to_monstros_ptr1(monstros, monstros_ptr, n_monstros);
// selecionar monstros com o tipo correspondente e escrevê-los na tela
Monstro *monstros_selecionados[n_monstros];
char tipo_pesquisa[100];
scanf("%s", tipo_pesquisa);
int n_monstros_selecionados = pesquisa_monstros_tipo(monstros_ptr, n_monstros, monstros_selecionados, tipo_pesquisa);
for (int i = 0; i < n_monstros_selecionados; i++) {
escrever_monstro(monstros_selecionados[i]);
}
}
int main() {
testF();
return 0;
}
I think the problem might be how I used the inputs that went into the struct programming in C but I'm not sure. Can anyone help me?