I am working on a code to write and read information from a struct that has a pointer type variable and everything works well so far but, when i try to clean the screen with system("CLS"); the program exits but this only happens after reading the string persona[cx].nombre.
I tried using different ways of reading the string (getchar(), scanf(" %s") and scanf(" %c)) but that doesn't seems to help. I guess that this problem is something related to the pointer variable because pointers and structs are both new for me so maybe i am missing something.
here is the code i've made so far:
struct datos{
char nombre[30];
int edad;
char sexo;
} *persona;
void arreee(int *);
int main(){
int me = 0;
persona = malloc(sizeof(char[1]));
printf("\n Repeticiones: ");
scanf("%d", &me);
fflush(stdin);
persona = realloc(persona, me);
arreee(&me);
printf("\n. . .\nDone! %d\n. . .\n>", me);
getchar();
return 0;
}
// funciones
void arreee(int *e){
int cx, i;
for(cx = 0; cx < *e; cx++)
{
printf("\nsexo? m/f\n> ");
scanf("%1c", &persona[cx].sexo);
fflush(stdin);
printf("\nnombre> ");
fgets(persona[cx].nombre, 30, stdin);
fflush(stdin);
printf("\nedad> ");
scanf("%d", &persona[cx].edad);
fflush(stdin);
system("cls");
}
for(i = 0; i < *e; i++)
{
printf("Sexo %c Edad %d Nombre %s", persona[i].sexo, persona[i].edad, persona[i].nombre);
}
}