Ver Mensaje Individual
  #5 (permalink)  
Antiguo 11/06/2010, 04:04
minette1988
 
Fecha de Ingreso: febrero-2010
Mensajes: 258
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: imprimir la deuda mayor de un cliente

Por mas que lo intento no me sale la deuda más alta.

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LEE_CHAR(c)\
  6.         c=getchar();\
  7.         while((c=='\n') || (c==' ') || (c=='\t'))\
  8.             c=getchar();
  9.  
  10. #define LEE_CAD(c,n) { int i=0; \
  11.                      c[i]=getchar();\
  12.              while((c[i]=='\n') || (c[i]=='\t')) c[i]=getchar();\
  13.              while ((c[i]!='\n') && (i < (n-1))){ \
  14.                 i++;\
  15.             c[i]=getchar(); }\
  16.              c[i]='\0'; }
  17.  
  18. #define TAM_NOMBRE 100
  19. #define TAM_DIR 100
  20.  
  21. struct cliente{
  22.     char nombre[TAM_NOMBRE];
  23.         char direccion[TAM_DIR];
  24.         double deuda;
  25.         double mayor_deuda;
  26. };
  27. int main(){
  28.     void crear_fich();
  29.         void deuda_mayor();
  30.         char sel;
  31.     FILE *pf; //Puntero a fichero
  32.  
  33.         /* Abre el fichero para trabajar con él en Lectura / Escritura */
  34.         if((pf = fopen("datos.dat", "wb+")) == NULL) {/*"rb+"*/
  35.            /* Si no existe, ejecuta el módulo que lo cree */
  36.        crear_fich();
  37.        /* Una vez creado lo habre en lectura/escritura */
  38.            if((pf = fopen("datos.dat", "rb+")) == NULL) {
  39.         perror("Fichero no accesible");
  40.                 exit(1);
  41.        }
  42.         }
  43.  
  44.     do{
  45.         printf("MENÚ\n----\n");
  46.        
  47.         printf("1). Dar de alta un cliente.\n");
  48.                 printf("2). Mostrar la deuda mayor.\n");
  49.                 printf("3). salir y eliminar.\n");
  50.                 printf("0). Salir\n\nOpción(0-3): ");
  51.  
  52.         do{
  53.             LEE_CHAR(sel);
  54.         }while( (sel<'0') || (sel>'3') );
  55.  
  56.         switch(sel){
  57.                 case '1':{
  58.                         crear_fich(pf);
  59.                         /* Una vez creado lo abre en lectura/escritura */
  60.                         if((pf = fopen("datos.dat", "rb+")) == NULL) {
  61.                         perror("Fichero no accesible");
  62.                                 exit(2);
  63.                         }
  64.                     break;}
  65.                                 case '2':{ deuda_mayor(pf);
  66.                                         break;}
  67.                                 case '3':{ fclose(pf); pf=NULL; remove("datos.dat"); sel= '0';
  68.                     break;}
  69.                 } //switch
  70.     }while(sel!='0');
  71.  
  72.     exit(0);
  73. }//main()
  74. ////////////////////////////////////////
  75. void crear_fich(FILE* pf) {
  76.          struct cliente cli;
  77.          char otro;
  78.  
  79.      if((pf = fopen("datos.dat", "ab")) == NULL) {
  80.         perror("Al crear el fichero de datos");
  81.                 exit(2);
  82.          }
  83.          
  84.          /* Lectura de los ciclistas */
  85.          do {
  86.        
  87.         cli.deuda = 0.0;
  88.                 printf("Nombre: "); LEE_CAD(cli.nombre, TAM_NOMBRE);
  89.                 printf("Dirección: "); LEE_CAD(cli.direccion, TAM_DIR);
  90.                 printf("Deuda: "); scanf("%lf", &cli.deuda);
  91.                 /*Escribe el cliente en el fichero */
  92.                 fwrite(&cli, sizeof(struct cliente), 1, pf);
  93.         printf("¿Otro? (s/n) ");
  94.                 LEE_CHAR(otro);
  95.         }while((otro=='s') || (otro=='S'));
  96.     if(fclose(pf) == EOF){
  97.         printf("Error al cerrar el fichero, compruebe si hay información.\n"); 
  98.                 exit(3);
  99.         }
  100. } /* crear_fich()*/
  101. //////////////////////////////////
  102. void deuda_mayor(FILE *pf){
  103.         struct cliente cli;
  104.          
  105.         int mayor_deuda;
  106.         double deuda;
  107.        
  108.         if((pf=fopen("datos.dat","rb"))==NULL){
  109.            printf("Error al abrir el fichero.\n");
  110.            exit(3);
  111.         }    
  112.         fread(&cli,sizeof(struct cliente),1,pf);
  113.         while(!feof(pf)){
  114.            fread(&cli,sizeof(struct cliente),1,pf);
  115.            if(cli.deuda > cli.mayor_deuda);
  116.               deuda=mayor_deuda;
  117.         }
  118.         if(fclose(pf) == EOF){
  119.         printf("Error al cerrar el fichero, compruebe si hay información.\n"); 
  120.                 exit(4);
  121.         }
  122.         printf("%s %.2lf\n",cli.nombre,cli.deuda);
  123. }

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