Dopo la fgets prova con

if(temp[strlen(temp)-1] != '\n') while(getchar() != '\n');