Raga mi da dei problemi. Serve a inserire i dati di 100 persone. Quando scrivo le date anche se vado fuori intervallo non mi fa riscrivere la data esatta e anche se inserisco un reddito annuale poi quando deve visualizzare il rapporto mi da un errore Null pointer assignement e divide error. Cosa c'è di sbagliato?
L'ho copiato dal mio libro di C ed è corretto perchè quando compilo non da errori solo quando eseguo mi da questi errori.
Ciao
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define YES 1
#define NO 0
struct record {
char fname[15+1];
char lname[20+1];
char phone[9+1];
long income;
int month;
int day;
int year;
};
struct record list[MAX];
int last_entry = 0;
int main(void);
void get_data(void);
void display_report(void);
int continue_function(void);
void clear_kb(void);
int main()
{
int cont = YES;
int ch;
while( cont == YES)
{
printf("\n");
printf("\n MENU");
printf("\n ========\n");
printf("\n1. Inserisci nomi");
printf("\n2. Stampa rapporto");
printf("\n3. Esci");
printf("\n\nInserire selezione ==> ");
ch = getchar();
fflush(stdin);
switch( ch )
{
case '1': get_data();
break;
case '2': display_report();
break;
case '3': printf("\n\nGrazie per avere usato il programma!!\n");
cont = NO;
break;
default: printf("\n\nScelta non valida. Serve un numero da 1 a 3!");
break;
}
return 0; }
}
void get_data(void)
{
int cont;
for ( cont = YES; last_entry < MAX && cont == YES; last_entry++)
{
printf("\n\nInserire dati per persona %d.",last_entry+1);
printf("\n\nInserire nome: ");
gets(list[last_entry].fname);
printf("\nInserire cognome: ");
gets(list[last_entry].lname);
printf("\nInserire telefono in formato 123-4567: ");
gets(list[last_entry].phone);
printf("\nInserire reddito annuale: ");
scanf("%ld", &list[last_entry].income);
printf("\nInserire data di nascita:");
do
{
printf("\n\tGiorno (0 - 31): ");
scanf("%d",list[last_entry].day);
}while ( list[last_entry].day < 0 ||
list[last_entry].day > 31 );
do
{
printf("\n\tMese (0 - 12): ");
scanf("%d",list[last_entry].month);
}while ( list[last_entry].month < 0 ||
list[last_entry].month > 12 );
do
{
printf("\n\tAnno (1800 - 2003): ");
scanf("%d",list[last_entry].year);
}while (list[last_entry].year != 0 &&
(list[last_entry].year < 1800 ||
list[last_entry].year > 2003 ));
cont = continue_function();
if (cont == 0)
main();
}
if( last_entry == MAX)
printf("\n\nMassimo numero di nomi raggiunto!\n");
}
void display_report()
{
long month_total = 0,
grand_total = 0;
int x, y;
fprintf(stdout, "\n\n");
fprintf(stdout, "\n RAPPORTO");
fprintf(stdout, "\n =========");
for( x = 0; x <=12; x++ )
{
month_total = 0;
for( y = 0; y < last_entry; y++ )
{
if( list[y].month ==x )
{
fprintf(stdout, "\n\t%s %s %s %ld",list[y].fname,
list[y].lname, list[y].phone,list[y].income);
month_total += list[y].income;
}
}
fprintf(stdout, "\nTotale per mese %d Š %ld",x,month_total);
grand_total += month_total;
}
fprintf(stdout, "\n\nTotali:");
fprintf(stdout, "\nIl reddito totale Š %ld", grand_total);
fprintf(stdout, "\nIl reddito medio Š %ld", grand_total/last_entry);
fprintf(stdout, "\n\n* * * Fine rapporto * * *\n");
system("pause");
}
int continue_function( void )
{
int ch;
printf("\n\nVuoi continuare? (Y)es/(N)o: ");
fflush(stdin);
ch = getchar();
while( ch != 'n' && ch != 'N' && ch != 'Y' && ch !='y' )
{
printf("\n%c non Š valido!", ch);
printf("\n\nInserire \'N\' per uscire o \'Y\' per continuare: ");
fflush(stdin);
ch = getchar();
}
clear_kb();
if(ch == 'n' || ch == 'N')
return(NO);
else
return(YES);
}
void clear_kb(void)
{
char junk[80];
gets(junk);
}