So I have written this simple C program which reads some information in a loop, and then asks the user if the input he gave is correct, but the last input (y\n) is never red from the console.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    char *name=malloc(sizeof(char)*20),*surname=malloc(sizeof(char)*20),awnser,c;
    int age,tries=1;
    while(1){
        printf("try %d\n",tries++);
        printf("enter name: ");             
        fgets(name,19,stdin);
        strtok(name,"\n");
        printf("enter surname: ");  
        fgets(surname,19,stdin);
        strtok(surname,"\n");
        printf("enter age: ");
        scanf("%d",&age);
        printf("so you are %s %s %d years old? (y/n)\n",name,surname,age);
        awnser=getchar();
        if(awnser=='y'){
            break;
        }
    }
    free(surname);
    free(name);
}
Expected output:
try 1 
enter name: bill 
enter surname: mpris 
enter age: 344 
so you are bill mpris 344 years old? (y/n) 
n 
try 2 
enter name: bill 
enter surname: 
moris enter age: 34 
so you are bill moris 34 years old? (y/n) 
y
Output of the program:
try 1
enter name: bill
enter surname: moris
enter age: 34 
so you are bill moris 34 years old? (y/n)
try 2
enter name:
Does anybody know why that happens? I tried putting fflush(stdin) after every fgets but i still had the same error. I can imagine that it has to do with the buffer and that some character ('\n' probably) is getting read but the getchar
