Hola chicos buenas.. soy nuevo en el foro, en verdad no puedo detectar el error en este programa, el compilador tampoco, solo que se me produce algun tipo de saturacion en memoria en plena ejecucion creo.. no se de donde viene el problema, les dejo todo el codigo, es largo disculpen.. espero que me puedan ayudar..
[center][left][center]#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct
{
int d,m,a;
} t_fecha;
typedef struct
{
char nyap[40];
t_fecha fnac;
int edad;
char tel[15];
} t_datos;
char menu (const char [][40]);
char pedir_opcion (const char [][40],const char *msj);
void Cargar(void);
void Mostrar(void);
void Ordenar(void);
void Alta(t_datos*);
char Preguntar (const char*);
int Correcta (t_fecha,t_fecha);
int SonDigitos (char*);
long int feccmp (t_fecha,t_fecha);
int Edad (t_fecha,t_fecha);
int main (void)
{
char op;
const char opciones [][40] = {
"CMOS",
"Cargar archivo.",
"Mostrar archivo.",
"Ordenar archivo por fec de nac.",
"Salir"
} ;
op=menu(opciones);
while(op!='S')
{
switch(op)
{
case 'C': Cargar(); break;
case 'M': Mostrar(); break;
case 'O': Ordenar();
}
op=menu(opciones);
}
return 0;
}
char menu (const char m[][40])
{
char op;
op=pedir_opcion(m,"\nIngrese su opcion: ");
while(!strchr(m[0],op))
op=pedir_opcion(m,"\nMal ingreso de opcion. Ingrese nuevamente: ");
return op;
}
char pedir_opcion (const char m[][40],const char *msj)
{
char op;
int i;
printf("\nMenu:");
for (i=0 ; i<strlen(m[0]) ; i++)
printf("\n %c - %s", m[0][i],m[i+1]);
printf("%s",msj);
fflush(stdin);
fscanf(stdin,"%c",&op);
return toupper(op);
}
void Cargar(void)
{
FILE *pf;
t_datos per;
char res = 'S';
pf = fopen ("C:\\Archivos.txt","w");
while(res=='S')
{
Alta(&per);
printf("%d|%s|%d/%d/%d|%s\n",per.edad,per.nyap,per.fnac.d,per.fnac.m,per.fnac.a,per.tel);
fprintf(pf,"%d|%s|%d/%d/%d|%s\n",per.edad,per.nyap,per.fnac.d,per.fnac.m,per.fnac.a,per.tel);
res = Preguntar ("\nOtro registro? S/N: ");
}
fclose(pf);
}
char Preguntar (const char *msj)
{
char op;
printf("%s");
fflush(stdin);
scanf("%c",&op);
op=toupper(op);
if (op != 'S' && op != 'N')
Preguntar ("\nIngreso una opcion erronea. Otro registro ? S/N: ");
return op;
}
void Alta(t_datos *per)
{
t_fecha hoy = {26,4,2011} ;
printf("\nIngrese los datos correctamente: ");
printf("\nIngresa nombre y apellido: ");
fflush(stdin);
gets(per->nyap);
printf("\nIngresa fecha de nacimiento: ");
fflush(stdin);
scanf("%d %d %d",&per->fnac.d,&per->fnac.m,&per->fnac.a);
while(!Correcta(per->fnac,hoy))
{
printf("\nIngreso mal la fecha de nacimiento. Ingrese nuevamente: ");
fflush(stdin);
scanf("%d %d %d",&per->fnac.d,&per->fnac.m,&per->fnac.a);
}
per->edad = Edad(per->fnac,hoy);
printf("\nIngresa numero de telefono: ");
fflush(stdin);
gets(per->tel);
while(!SonDigitos(per->tel))
{
printf("\nIngreso mal el telefono. Ingrese nuevamente: ");
fflush(stdin);
gets(per->tel);
}
}
int Correcta (t_fecha fec,t_fecha hoy)
{
int dias_x_mes[] = {31,28,31,30,31,30,31,31,30,31,30,31} ;
if (fec.m == 2) dias_x_mes[1]=29 ;
if((fec.d < 0 || fec.d > dias_x_mes[fec.m-1]) || (fec.m <0 || fec.m>12) || feccmp(hoy,fec) < 0 ) return 0;
return 1;
}
int SonDigitos (char *cad)
{
int i;
for (i=0; i<strlen(cad); i++)
if(cad[i]!='-' && (cad[i]<48 || cad[i]>57)) return 0;
return 1;
}
long int feccmp (t_fecha f1,t_fecha f2)
{
long int fecha1=f1.a*10000+f1.m*100+f1.d;
long int fecha2=f2.a*10000+f2.m*100+f2.d;
return fecha1-fecha2;
}
int Edad (t_fecha fec,t_fecha hoy)
{
int edad = hoy.a-fec.a;
if(hoy.m<fec.m) edad--;
if(hoy.m==fec.m && hoy.d<fec.d) edad--;
return edad;
}
void Ordenar(void)
{
// en construccion
return;
}
void Mostrar(void)
{
// en construccion
return;
}[/center][/left][/center]
Creo que tendrías que decir en qué parte del código falla, o por lo menos darnos una pista. :D
Hola
Al compilarlo da el siguiente warning
In function 'Preguntar':
line: 106 too few arguments for format
El warning lo da en
printf("%s");
de la función Preguntar ya que está esperando un string que no le das.
Saludos
Le estás diciendo que vas a mostrar por pantalla una variable de tipo cadena, pero no le dices cuál.
Por ejemplo si tienes una que se llame cadena tendrías que poner
printf("%s", cadena);
Seguramente con
printf("%s");
querías decir
printf("%s",msj);
Estás usando gets() que está obsoleta, cambiala por fgets(), además de que no estás limpiando el buffer de entrada.
Ahhh muchachos, muchisimas gracias !!!!
me anduvo perfecto ahora!! un saludo y espero que los pueda ayudar si necesitan algo !