Originally posted by lockpatrick
Sorry for being so fussy.
No problemo, if we stop answering your questions you know it's too fussy. Here's something you can try. Check all characters in your buffer with the isdigit function (but not the last character because this is the newline character).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( void )
{
unsigned int i;
char buffer[BUFSIZ];
printf ( "Enter a number: " );
if ( fgets ( buffer, sizeof buffer, stdin ) != NULL )
{
/* watch out for the ';' at the end of the next line (for) */
for(i = 0; (i < strlen(buffer)-1) && isdigit(buffer[i]); i++) ;
if(i == strlen(buffer)-1)
printf ( "%d\n", atoi ( buffer ) );
else
printf("Invalid number\n");
}
else
printf ( "Error: Input validation\n" );
return 0;
}