I have this code that won't work correctly if there is no getchar()
in the loop
#include <stdio.h>
#include <string.h>
main(void) {
typedef struct personne{
char nom [20] ;
char prenom [15] ;
int age ;
char tel[12] ;
} personne ;
personne p;
FILE * f ;
if ((f = fopen("Contacts.dat", "ab")) == NULL) {
printf("\nImpossible de lire le fichier commandes.dat\n");
}
else{
printf (" Saisie des contacts a stocker dans le fichier\n");
printf (" --- pour finir la saisie, donnez un nom ‘vide' ---\n");
while ( printf("nom : "), gets(p.nom), strlen(p.nom)!=0 ){
printf ("prenom : ") ;
gets(p.prenom) ;
printf ("age : ") ;
scanf("%d", &p.age) ;
printf("telephone : ") ;
//gets(p.tel) ;
scanf("%s", &p.tel) ;
fwrite(&p, sizeof(personne), 1, f);
getchar(); //This is the problem
}
fclose (f) ;
}}
If i keep getchar()
the output is normal:
But if i remove it then it stops after one turn :
please can someone explain why is this happening ?