#include <stdio.h>
int main()
{
char name[10];
for(int i=0;i<=10;i++)
{
printf("Who are you? ");
if(fgets(name,10,stdin)!=NULL)
printf("Glad to meet you, %s.\n",name);
}
return(0);
}
When I input something greater than 10 characters the loop skips.
Who are you? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Glad to meet you, aaaaaaaaa.
Who are you? Glad to meet you, aaaaaaaaa.
Who are you? Glad to meet you, aaaaaaaaa.
Who are you? Glad to meet you, aaaaaaaaa.
Who are you? Glad to meet you, aaaaaaaaa.
Who are you? Glad to meet you, aaaaaaaaa.
Who are you? Glad to meet you, aaaaaaa
I guess I want to clear the input buffer from the remaining characters. What will be the best way to do that...?
scanf("%*[^\n]");scanf("%*c");
rather than the incorrectscanf("%*[^\n]%*c");
– Parolee