You helped me a while ago with reading a line. Now, I want to read only digits from input - no letters, just 5 digits. How can I do this?
My solution doesn't work properly:
int i = 0;
while(!go)
{
printf("Give 5 digits: \n\n");
while( ( c = getchar()) != EOF && c != '\n' && i < 5 )
{
int digit = c - '0';
if(digit >= 0 && digit <= 9)
{
input[i++] = digit;
if(i == 5)
{
break;
go = true;
}
}
}
}
With the
break
statement,go = true;
will never be executed. Therefore the loopwhile (!go)
is infinite.Try with this: