Por mas que lo intento no me sale la deuda más alta.
Código C:
Ver original#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEE_CHAR(c)\
c=getchar();\
while((c=='\n') || (c==' ') || (c=='\t'))\
c=getchar();
#define LEE_CAD(c,n) { int i=0; \
c[i]=getchar();\
while((c[i]=='\n') || (c[i]=='\t')) c[i]=getchar();\
while ((c[i]!='\n') && (i < (n-1))){ \
i++;\
c[i]=getchar(); }\
c[i]='\0'; }
#define TAM_NOMBRE 100
#define TAM_DIR 100
struct cliente{
char nombre[TAM_NOMBRE];
char direccion[TAM_DIR];
double deuda;
double mayor_deuda;
};
int main(){
void crear_fich();
void deuda_mayor();
char sel;
FILE *pf; //Puntero a fichero
/* Abre el fichero para trabajar con él en Lectura / Escritura */
if((pf
= fopen("datos.dat", "wb+")) == NULL
) {/*"rb+"*/ /* Si no existe, ejecuta el módulo que lo cree */
crear_fich();
/* Una vez creado lo habre en lectura/escritura */
if((pf
= fopen("datos.dat", "rb+")) == NULL
) { perror("Fichero no accesible"); }
}
do{
printf("1). Dar de alta un cliente.\n"); printf("2). Mostrar la deuda mayor.\n"); printf("3). salir y eliminar.\n"); printf("0). Salir\n\nOpción(0-3): ");
do{
LEE_CHAR(sel);
}while( (sel<'0') || (sel>'3') );
switch(sel){
case '1':{
crear_fich(pf);
/* Una vez creado lo abre en lectura/escritura */
if((pf
= fopen("datos.dat", "rb+")) == NULL
) { perror("Fichero no accesible"); }
break;}
case '2':{ deuda_mayor(pf);
break;}
case '3':{ fclose(pf
); pf
=NULL
; remove("datos.dat"); sel
= '0'; break;}
} //switch
}while(sel!='0');
}//main()
////////////////////////////////////////
void crear_fich(FILE* pf) {
struct cliente cli;
char otro;
if((pf
= fopen("datos.dat", "ab")) == NULL
) { perror("Al crear el fichero de datos"); }
/* Lectura de los ciclistas */
do {
cli.deuda = 0.0;
printf("Nombre: "); LEE_CAD
(cli.
nombre, TAM_NOMBRE
); printf("Dirección: "); LEE_CAD
(cli.
direccion, TAM_DIR
); /*Escribe el cliente en el fichero */
fwrite(&cli
, sizeof(struct cliente
), 1, pf
); LEE_CHAR(otro);
}while((otro=='s') || (otro=='S'));
printf("Error al cerrar el fichero, compruebe si hay información.\n"); }
} /* crear_fich()*/
//////////////////////////////////
void deuda_mayor(FILE *pf){
struct cliente cli;
int mayor_deuda;
double deuda;
if((pf
=fopen("datos.dat","rb"))==NULL
){ printf("Error al abrir el fichero.\n"); }
fread(&cli
,sizeof(struct cliente
),1,pf
); fread(&cli
,sizeof(struct cliente
),1,pf
); if(cli.deuda > cli.mayor_deuda);
deuda=mayor_deuda;
}
printf("Error al cerrar el fichero, compruebe si hay información.\n"); }
printf("%s %.2lf\n",cli.
nombre,cli.
deuda); }
Cuando compilo no me sale la deuda más alta.
Nombre: ana
Dirección: sss
Deuda: 500.55
¿Otro? (s/n) s
Nombre: pepe
Dirección: ddd
Deuda: 100.45
¿Otro? (s/n) s
Nombre: lola
Dirección: ppp
Deuda: 10.21
¿Otro? (s/n) n
Resultado: lola 10.21
------------------------------------------
Nombre: pepe
Dirección: sss
Deuda: 10.21
¿Otro? (s/n) s
Nombre: ana
Dirección: ooo
Deuda: 500.55
¿Otro? (s/n) s
Nombre: dani
Dirección: ttt
Deuda: 96.32
¿Otro? (s/n) n
Resultado: dani 96.32
**********************************************
Nombre: pepe
Dirección: ddd
Deuda: 100.55
¿Otro? (s/n) s
Nombre: ana
Dirección: lll
Deuda: 200.74
¿Otro? (s/n) s
Nombre: dani
Dirección: ooo
Deuda: 500.55
¿Otro? (s/n) n
Resultado: dani 500.55