0

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?

  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Feb 22 '23 at 13:31

1 Answers1

1

You only select "monsters" on type using pesquisa_monstros_tipo.

You also need to select on CR with pesquisa_monstros_cr.

For example you could first call pesquisa_monstros_tipo to get the correct type (like you do now), but then add a call to pesquisa_monstros_cr with the data returned by pesquisa_monstros_tipo:

// First fetch monsters of a specific type
Monstro *monstros_selecionados_tipo[n_monstros];
int n_monstros_selecionados_tipo = pesquisa_monstros_tipo(monstros_ptr, n_monstros, monstros_selecionados_tipo, tipo_pesquisa);

// Then from the selected types, do a new search for the CR range
Monstro *monstros_selecionados_cr[n_monstros_selecionados_tipo];
int n_monstros_selecionados_cr = pesquisa_monstros_cr(monstros_selecionados_tipo, n_monstros_selecionados_tipo, monstros_selecionados_cr, 0.0, 1.0);

Now use n_monstros_selecionados_cr and monstros_selecionados_cr for your printing loop:

for (int i = 0; i < n_monstros_selecionados_cr; i++) {
    escrever_monstro(monstros_selecionados_cr[i]);
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621