I am practicing doing some programs on struct before I start practicing dynamic memory and I have been having this problem. This program is supposed to be simple, I have a Monster struct with the information that each monster will have and my main goal was to be able to create search functions to search monsters with a certain type with certain hp etc but I decided to start making simpler to test and simply print the monsters that were added, I start by saying how many monsters will be added (n) and then add their information, the problem is that my output gives like this :
Input:
2
Alexandre Orc Medium 10 10 10
Jose Human Large 10 10 10
Output:
Alexandre - Orc 1984069797 6421572 -1113634697
Medium - 10 10 6421572 -1113634697
And it should appear :
Alexander Orc Medium 10 10
Jose Human Large 10 10 10
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <math.h>
typedef enum { Tiny, Small, Medium, Large, Huge, Gargantuan } Tamanho;
struct Monstro {
char nome[100];
char tipo[100];
Tamanho Tam;
int ac;
int hp;
int cr;
};
struct Monstro monstros(char *nome, char *tipo, Tamanho Tam, int ac, int hp, int cr) {
struct Monstro m;
strcpy(m.nome, nome);
strcpy(m.tipo, tipo);
m.Tam = Tam;
m.ac = ac;
m.hp = hp;
m.cr = cr;
return m;
}
const char* tamanho_para_string(Tamanho tam)
{
switch(tam)
{
case Tiny:
return "Tiny";
case Small:
return "Small";
case Medium:
return "Medium";
case Large:
return "Large";
case Huge:
return "Huge";
case Gargantuan:
return "Gargantuan";
default:
return "";
}
}
int get_monstros(struct Monstro *a, int n) {
char nome[100];
char tipo[100];
Tamanho Tam;
int ac;
int hp;
int cr;
int i = 0;
while (i < n && scanf("%s %s %u %i %i %i", nome, tipo, &Tam, &ac, &hp, &cr) != EOF)
{
a[i++] = monstros(nome, tipo, Tam, ac, hp, cr);
}
return i;
}
void println_monstros(struct Monstro *m)
{
printf("%s - %s %s %d %d %d\n", m->nome, m->tipo, tamanho_para_string(m->Tam), m->ac, m->hp, m->cr);
}
void testF(void) {
int n;
scanf("%d", &n);
struct Monstro a[n];
int i = get_monstros(a, n);
for (int j = 0; j < i; j++) {
println_monstros(&a[j]);
}
}
int main() {
testF();
return 0;
}