You should empty the stdin
buffer before read something else.
void emptyBuffer() {
char c="a";
while (c != '\n' && c != EOF) {
c = getchar();
}
return;
}
This function empty the stdin buffer.
printf("Enter your name: ");
scanf("%s", &string);
emptyBuffer();
fprintf(fw, "%d\n%s\n",p, string);
printf("Enter your telephone number: ");
scanf("%d",&cislo);
emptyBuffer();
fprintf(fw, "%d\n",cislo);
printf("If u want to add more datas press Y otherwise press N ");
c = getchar();
Because when you use getchar()
, you probably get the last '\n'
of the previous question (/answer).
And BTW (according to previous comments)
you should use:
if ( c == 'Y' || c == 'y' )
which is more user friendly 🙂
4
solved Yes/no does not work [closed]