Ciao a tutti...volevo chiedervi un aiuto riguardo questo programma. Non riesco a capire come gestire la funzione contalibro, perchè quando compilo forse non entra nel ciclo while perchè appena arriva a questo punto esce dal programma. Come posso risolverlo?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
struct biblioteca
{
char titolo [N];
char autore[N];
int numero_volumi;
struct biblioteca *next;
};
typedef struct biblioteca biblioteca;
biblioteca *add_in_order(biblioteca *head, char temp_titolo[], char temp_autore [], int temp_numero_volumi);
void insert(biblioteca *a,biblioteca *b, biblioteca *c);
void cercalibro (biblioteca *head2);
void contalibro (biblioteca *head3);
int main ()
{
char temp_titolo[N];
char temp_autore[N];
int temp_numero_volumi;
int nstruct;
int out;
biblioteca *head = NULL, *head1 = NULL, *head2=NULL, *head3=NULL;
while (1)
{
printf("Inserisci il titolo del libro, digita EXIT per uscire\n");
scanf("%s", temp_titolo);
if(strcmp(temp_titolo, "EXIT")==0)
break;
printf("Inserisci l'autore\n");
scanf("%s", temp_autore);
printf("Inserisci il numero di volumi\n");
scanf("%d", &temp_numero_volumi);
head=add_in_order(head, temp_titolo, temp_autore, temp_numero_volumi);
nstruct++;
}
FILE *fp;
fp=fopen("biblioteca.txt", "w");
head1=head;
while (head1 != NULL)
{
printf("%s %s %d\n", head1->titolo, head1->autore, head1->numero_volumi);
fprintf(fp,"%s %s %d\n", head1->titolo, head1->autore, head1->numero_volumi);
head1= head1->next;
}
fclose(fp);
printf("Adesso leggero\' la lista salvata nel file\n");
fp=fopen("biblioteca.txt", "r");
while (1)
{
out=fscanf(fp, "%s %s %d", temp_titolo, temp_autore, &temp_numero_volumi);
if(out<0)
break;
printf("%s %s %d\n", temp_titolo, temp_autore, temp_numero_volumi);
}
fclose(fp);
head2=head;
cercalibro(head2);
head3=head;
printf("%p", head3);
contalibro(head3);
}//chiude main
//funzioni
biblioteca *add_in_order(biblioteca *head, char temp_titolo[], char temp_autore[], int temp_numero_volumi)
{
biblioteca *p,*temp_p,*prev_p;
p=(biblioteca*)calloc(1,sizeof(biblioteca));
strcpy(p->titolo,temp_titolo);
strcpy(p->autore,temp_autore);
p->numero_volumi=temp_numero_volumi;
p->next=NULL;
if(head==NULL)
return p;
if(strcmp(temp_titolo, head->titolo)<=0)
{
p->next=head;
return p;
}
else
{
prev_p=head;
temp_p=head;
while(temp_p->next!=NULL && strcmp(temp_titolo,temp_p->titolo)>0)
{
prev_p=temp_p;
temp_p=temp_p->next;
}
if(temp_p->next==NULL && strcmp(temp_titolo,temp_p->titolo)>0)
insert(temp_p,NULL,p);
else
insert(prev_p,temp_p,p);
}
return head;
}
void insert(biblioteca *a,biblioteca *b, biblioteca *c)
{
if(a->next!=b)
{
printf("Error\n");
return;
}
a->next=c;
c->next=b;
return;
}
void cercalibro (biblioteca *head2)
{
char title[N], cont=0;
printf("inserire titolo da cercare:\n");
scanf("%s", title);
FILE *fp;
fp=fopen("biblioteca.txt","r");
while (head2!=NULL)
{
if(strcmp(head2->titolo, title)==0)
{
printf("il libro cercato è: %s %s %d\n",head2->titolo, head2->autore, head2->numero_volumi);
cont++;
}
head2=head2->next;
}
if(cont==0)
puts("codice non trovato\n");
fclose(fp);
}
void contalibro (biblioteca *head3)
{
char letter[1], vect[N];
int numerolibri=0;
printf("inserire l'iniziale desiderata:\n");
scanf("%s",letter);
FILE *fp;
fp=fopen("biblioteca.txt", "r");
while (head3 != NULL)
{
strcpy(vect, head3->titolo);
printf("%s", vect);
if (letter[0]==vect[0])
{
numerolibri++;
printf("%d\n",numerolibri);
}
head3=head3->next;
}
}