I'm not sure I understand your question. If the user enters an invalid number, the program prints an error and prompts the user to retype the number. This process is continued for the same item until a valid number is entered, then the next item is recieved:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define BOOL_TRUE 1
#define BOOL_FALSE 0
static int valid ( char *buf, size_t bsize )
{
size_t i;
for ( i = 0; i < bsize - 1; i++ )
if ( !isdigit ( buf[i] ) )
return BOOL_FALSE;
return BOOL_TRUE;
}
int main (void)
{
int maxnum;
int a;
char buff[BUFSIZ];
printf("How many numbers would you like to sort?\n");
scanf("%d%*c", &maxnum);
printf("\nPlease begin entering your %d numbers.\n", maxnum);
/* GATHER DATA */
for( a = 0; a < maxnum; a++)
{
printf( "Enter number %d : ", a + 1 );
fgets( buff, BUFSIZ, stdin );
while( valid( buff, strlen(buff) ) == BOOL_FALSE )
{
printf( "Invalid Integer.\n" );
printf( "Please re-enter number %d: ", a + 1 );
fgets( buff, BUFSIZ, stdin );
}
#if 0
/* This won't work, buff is a char *, not a double */
sscanf( buff, "%lf", buff );
#endif
}
return 0;
}
-Prelude