Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2004
    Messaggi
    229

    [C]: stampa sempre stesso valore

    Perchè stampa sempre lo stesso valore????

    codice:
    # include <stdio.h>
    # include <time.h>
    
    main(){	
    int s_hour, s_minutes, s_second, e_hour, e_minutes, e_second,i;	
    struct tm * start = (struct tm*)malloc(sizeof(struct tm));
    struct tm * end = (struct tm*)malloc(sizeof(struct tm));
    double total;
    time_t start_t, end_t; 
    
    printf("Inserire ore, minuti e secondi");
    
    scanf("%d",&s_hour);
    scanf("%d",&s_minutes);
    scanf("%d",&s_second);
    
    printf("Inserire ore, minuti e secondi");
    
    scanf("%d",&e_hour);
    scanf("%d",&e_minutes);
    scanf("%d",&e_second);
    
    start->tm_hour=s_hour;
    start->tm_min=s_minutes;
    start->tm_sec=s_second;
    
    end->tm_hour=e_hour;
    end->tm_min=e_minutes;
    end->tm_sec=e_second;
    
    start_t= mktime(start);
    end_t=mktime(end);
    
    total= difftime(end_t,start_t);
    
    printf("I secondi sono:");
    printf("%d", &total);
    
    scanf("%d", &i);
    }
    Ad Maiora

  2. #2
    credo ci siano 2 errori, ma lo stesso non va:

    1 - Nell'istruzione "printf("%d", &total);", mettendo la & si stampa l'indirizzo della variabile total, non il contenuto
    2 - Nella stessa istruzione la conversione non dovrebbe essere %d, che rappresenta un intero, ma %f (mi pare) che rappresenta un float.

    In ogni caso ho provato ma dà sempre 0. Non so molto sul time_t, quindi se vuoi qui c'è lo stesso programma che fa un conto "tradizionale":


    codice:
    # include <stdio.h>
    # include <time.h>
    
    main(){
    int s_hour, s_minutes, s_second, e_hour, e_minutes, e_second,i;
    
    struct tm * start = (struct tm*)malloc(sizeof(struct tm));
    
    struct tm * end = (struct tm*)malloc(sizeof(struct tm));
    
    long total;
    time_t start_t, end_t;
    	clrscr();
    printf("Inserire ore, minuti e secondi");
    
    scanf("%d",&s_hour);
    scanf("%d",&s_minutes);
    scanf("%d",&s_second);
    
    printf("Inserire ore, minuti e secondi");
    
    scanf("%d",&e_hour);
    scanf("%d",&e_minutes);
    scanf("%d",&e_second);
    
    total = (e_hour - s_hour)*3600 + (e_minutes - s_minutes) * 60 + (e_second - s_second);
    
    
    printf("I secondi sono:");
    printf("%d", total);
    
    scanf("%d", &i);
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.