Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Non riesco a capire come correggere i seguenti errori/warning
Forum - C/C++ - Non riesco a capire come correggere i seguenti errori/warning

Avatar
lorenzoscarrone (Normal User)
Pro


Messaggi: 92
Iscritto: 16/11/2011

Segnala al moderatore
Postato alle 11:26
Mercoledì, 25/03/2015

Codice sorgente - presumibilmente C++

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define DATABASE "database.dat" //definisce il collegamento al File che contiene i dati
  6. #define lenght(array) (sizeof(array)/sizeof(array[0]))
  7. #define prezzo_f(time) (20+(time/60)*12)
  8.  
  9. void load_file_on_registro();
  10. void stampa_conto( int numero);
  11.  
  12. //serve per definire un array di stringhe
  13. struct String{
  14.     char cont[255];
  15.    } contenuto[4];
  16.  
  17. struct PhoneRegister{
  18.     int number;
  19.     char date[11];
  20.     int time;
  21.     char state[2];
  22. } registro[1000];
  23.  
  24. FILE *file = fopen(DATABASE, "r");
  25.  
  26. int main() {
  27.    
  28.     int numero;
  29.     scanf("%d", &numero);
  30.     load_file_on_registro();
  31.     stampa_conto(numero);
  32.     return EXIT_SUCCESS;
  33.    
  34. }
  35.  
  36. void load_file_on_registro(){
  37.    
  38.     int i,k = 0;
  39.     char linea[28];
  40.     char tk[2] = " ";
  41.     char *token;
  42.    
  43.     while ( !feof(file) )
  44.     {
  45.         fgets(linea, 28, file);
  46.        
  47.         if (linea)
  48.         {
  49.           while(token!=NULL){
  50.                strcpy(contenuto[i].cont, token);
  51.                token = strtok(NULL, tk);
  52.                i++;
  53.             }
  54.             i=0;
  55.             token = strtok(linea,tk);
  56.             registro[k].number = atoi(contenuto[0].cont);
  57.             strncpy(registro[k].date, contenuto[1].cont, 11);
  58.             registro[k].time   = atoi(contenuto[2].cont);
  59.             strncpy(registro[k].state, contenuto[3].cont, 2);
  60.         }
  61.        
  62.        k++;
  63.     }
  64.    
  65.     fclose(file);
  66. }
  67.  
  68. void stampa_conto(int numero){
  69.        
  70.         int i,k;
  71.         int tot_sec,c_lunga;
  72.         float min;
  73.         tot_sec = c_lunga = 0;
  74.        
  75.         for (i = 0; i<=lenght(registro); i++){
  76.                 if(numero == registro[i].number && registro[i].state == "U"){
  77.                         tot_sec += registro[i].time;
  78.                         if (c_lunga <= registro[i].time){
  79.                                 c_lunga = registro[i].time;
  80.                                 k = i;
  81.                                 }
  82.                 }      
  83.         }  
  84.        
  85.         printf("Minuti totali di chiamate in uscita: %f minuti\n", tot_sec/60);
  86.         printf("Chiamata in uscita più lunga: %f minuti effettuata il %s\n", c_lunga/60,registro[k].date);
  87.         printf("Costo totale chiamate in uscita: € %f", prezzo_f(tot_sec));
  88.        
  89. }



Compilatore:
Codice sorgente - presumibilmente Delphi

  1. Scrivania/main.c:32:1: error: initializer element is not constant
  2.  FILE *file = fopen(DATABASE, "r");
  3.  ^
  4. Scrivania/main.c: In function ‘stampa_conto’:
  5. Scrivania/main.c:93:2: warning: format ‘%f’ expects argument of typedouble, but argument 2 has typeint[-Wformat=]
  6.   printf("Minuti totali di chiamate in uscita: %f minuti\n", tot_sec/60);
  7.   ^
  8. Scrivania/main.c:94:2: warning: format ‘%f’ expects argument of typedouble, but argument 2 has typeint[-Wformat=]
  9.   printf("Chiamata in uscita più lunga: %f minuti effettuata il %s\n", c_lunga/60,registro[k].date);
  10.   ^
  11. Scrivania/main.c:95:2: warning: format ‘%f’ expects argument of typedouble, but argument 2 has typeint[-Wformat=]
  12.   printf("Costo totale chiamate in uscita: € %f", prezzo_f(tot_sec));



non so se può essere utile ma posto anche il database.dat

3472222222 01/01/2011 120 E
3472222222 02/01/2011 321 E
3471111111 01/01/2011 38 E
3472222222 08/01/2011 100 U
3471111111 01/02/2011 38 E
3473333333 01/02/2011 100 U
3472222222 10/01/2011 100 U
....

PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 16:35
Mercoledì, 25/03/2015
Codice sorgente - presumibilmente C/C++

  1. FILE *file = fopen(DATABASE, "r");



Non puoi aprire un file durante la compilazione. Se ti serve accesso globale a *file (il che è un problema di design già di suo), devi dichiararlo senza inizializzarlo.

Codice sorgente - presumibilmente C/C++

  1. FILE *file;
  2.  
  3. int main(){
  4.    file = fopen(...
  5. }




I vari warnings sono perchè stai usando dei numeri interi, mentre è chiaro che ti servono dei double. Ripassati il capitolo dove vengono trattati i cast impliciti.

Ultima modifica effettuata da pierotofy il 25/03/2015 alle 16:35


Il mio blog: https://piero.dev
PM Quote