I want to create a structure that grows in size when registering a new data.
It saves me the first three data well without any problem, when entering a fourth data the first data is deleted until entering a seventh data the second one is deleted.
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct{
char *nombre;
char *apellido;
}datos;
datos *dato;
void memDatos(int *tmx);
void reDatos(int *x);
void delSpace(char word[50]);
void memDinamic(char aux[50],int w, int d);
void editarDatos(int tmx);
int main(){
int op;
char key;
int tmx = 0;
memDatos(&tmx);// asignar tamanio de la estructura
do{
do{
printf("\n Digite un Comando;\n");
printf("(1) Registrar Datos\n");
printf("(2) Iniciar Busqueda\n");
printf("(3) Editar Datos\n >>> ");
scanf("%d",&op);
}while(op < 1 || op > 3);
switch(op){
case 1: reDatos(&tmx); break;
// case 2: busqueda(tmx); break;
case 3: editarDatos(tmx); break;
}
printf("\nTarea Finalizada\nDesea continuar (S/N) ");
fflush(stdin);
scanf("%c",&key);
}while(key == 's' || key == 'S');
system("pause>null");
return 0;
}
void memDatos(int *tmx){
dato = (datos*)malloc((*tmx+1)*sizeof(datos));// se registra el tamanio
if(dato == NULL) exit(1);
}
void reDatos(int *x){
char auxiliar[50];
int d=1;
printf("Nombre?: "); fflush(stdin);
fgets(auxiliar, 50, stdin);
delSpace(auxiliar); // eliminar el salto de linea de fgets
memDinamic(auxiliar,*x,d); d++; // asignar el tamanio adecuado para nombre
printf("Apellido?: "); fflush(stdin);
fgets(auxiliar, 50, stdin);
delSpace(auxiliar); memDinamic(auxiliar,*x,d);// x es la posicion a acceder en la estructura
(*x)++; // incrementar el tamanio de estructura
}
void delSpace(char word[50]){
int i;
for(i = 0; i < 50; i++){
if(word[i] == '\n'){
word[i] = '#include<string.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct{
char *nombre;
char *apellido;
}datos;
datos *dato;
void memDatos(int *tmx);
void reDatos(int *x);
void delSpace(char word[50]);
void memDinamic(char aux[50],int w, int d);
void editarDatos(int tmx);
int main(){
int op;
char key;
int tmx = 0;
memDatos(&tmx);// asignar tamanio de la estructura
do{
do{
printf("\n Digite un Comando;\n");
printf("(1) Registrar Datos\n");
printf("(2) Iniciar Busqueda\n");
printf("(3) Editar Datos\n >>> ");
scanf("%d",&op);
}while(op < 1 || op > 3);
switch(op){
case 1: reDatos(&tmx); break;
// case 2: busqueda(tmx); break;
case 3: editarDatos(tmx); break;
}
printf("\nTarea Finalizada\nDesea continuar (S/N) ");
fflush(stdin);
scanf("%c",&key);
}while(key == 's' || key == 'S');
system("pause>null");
return 0;
}
void memDatos(int *tmx){
dato = (datos*)malloc((*tmx+1)*sizeof(datos));// se registra el tamanio
if(dato == NULL) exit(1);
}
void reDatos(int *x){
char auxiliar[50];
int d=1;
printf("Nombre?: "); fflush(stdin);
fgets(auxiliar, 50, stdin);
delSpace(auxiliar); // eliminar el salto de linea de fgets
memDinamic(auxiliar,*x,d); d++; // asignar el tamanio adecuado para nombre
printf("Apellido?: "); fflush(stdin);
fgets(auxiliar, 50, stdin);
delSpace(auxiliar); memDinamic(auxiliar,*x,d);// x es la posicion a acceder en la estructura
(*x)++; // incrementar el tamanio de estructura
}
void delSpace(char word[50]){
int i;
for(i = 0; i < 50; i++){
if(word[i] == '\n'){
word[i] = '%pre%';
}
}
}
void memDinamic(char aux[50],int x, int d){
switch(d){
case 1: dato[x].nombre = (char*)malloc((strlen(aux)+1)*sizeof(char));
strcpy(dato[x].nombre,aux); break;
default: dato[x].apellido = (char*)malloc((strlen(aux)+1)*sizeof(char));
strcpy(dato[x].apellido,aux); break;
}
}
void editarDatos(int tmx){
int i;
puts("Usuarios registrados, seleccione alguno para continuar\n");
for(i = 0; i < tmx; i++){
printf("(%i) %s %s\n",i+1,dato[i].nombre,dato[i].apellido);
}
}
';
}
}
}
void memDinamic(char aux[50],int x, int d){
switch(d){
case 1: dato[x].nombre = (char*)malloc((strlen(aux)+1)*sizeof(char));
strcpy(dato[x].nombre,aux); break;
default: dato[x].apellido = (char*)malloc((strlen(aux)+1)*sizeof(char));
strcpy(dato[x].apellido,aux); break;
}
}
void editarDatos(int tmx){
int i;
puts("Usuarios registrados, seleccione alguno para continuar\n");
for(i = 0; i < tmx; i++){
printf("(%i) %s %s\n",i+1,dato[i].nombre,dato[i].apellido);
}
}