Ok I went through and made a new example of a integer number input using a custom function which is modified from the last FAQ example code. I added it to the bottom of my other examples to compare them. It is interesting that the custom function is the only one of the three that does not fail in some part. I see now how much better it is to use fgets into a string because of the ability to check the entire string for correctness before converting the string into a int. The great part is I don't have to worry about junk in the stdin left over after the call. The only drawback I have right now is if the user hits the enter key without typing anything it returns 0 instead of calling that an error and waiting for more input.
Here is my code with output at the end
Code:
/*
* File: numbinput.c
* Author: jim
*
* Created on June 5, 2010, 11:50 AM
*/
/* *******************************************************
* Testbed for number input routines
*************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MIN_NUMBER 0
#define MAX_NUMBER 20
#define MIN_FLOAT 0.01
#define MAX_FLOAT 20.00
#define MIN_SNUMBER 0
#define MAX_SNUMBER 50
#define CURSOR ">>>"
int UserInput(int min, int max);
double UserInputFloat(double min, double max);
int UserInputStrgNumb(int min, int max);
int Validate(char *a);
int main(void){
int number;
double fnumber;
int snumber;
printf("Enter a number for UserInput (0-20)\n");
number = UserInput(MIN_NUMBER, MAX_NUMBER);
printf("Enter number for UserInputFloat (0.00-20.00)\n");
fnumber = UserInputFloat(MIN_FLOAT, MAX_FLOAT);
printf("Enter a number for UserInputStrgNumb (0-50)\n");
snumber = UserInputStrgNumb(MIN_SNUMBER, MAX_SNUMBER);
printf("The number you entered for UserInput = %d\n",number);
printf("The number you entered for UserInputFloat = %.2f\n",fnumber);
printf("The number you entered for UserInputStrgNumb = %d\n",snumber);
return 0;
}
/***********************************
* UserInput
* Parameters:
* min: minimum number to except
* max: maximum number to except
* Returns: integer number in the range (min,max)
* Prints: nothing
***********************************/
int UserInput(int min, int max){
int input;
input = 0;
printf(CURSOR);
while (scanf("%d",&input) != 1 || input < min || input > max){
while (getchar() != '\n');
printf(CURSOR);
}
while (getchar() != '\n');
return input;
}
/***********************************
* UserInputFloat
* Parameters:
* min: minimum number to except
* max: maximum number to except
* Returns: double floating point number in the range (min,max)
* Prints: nothing
***********************************/
double UserInputFloat(double min, double max){
double input;
input = 0.0;
printf(CURSOR);
while (scanf("%lf",&input) != 1 || input < min || input > max ){
while (getchar() != '\n');
printf(CURSOR);
}
while (getchar() != '\n');
return input;
}
/***********************************
* UserInputStrgNumb
* Parameters:
* min: minimum number to except
* max: maximum number to except
* Returns: integer number in the range (min,max)
* Prints: nothing
***********************************/
int UserInputStrgNumb(int min, int max){
int i;
int error = 1;
char buffer[BUFSIZ];
printf(CURSOR);
while(error == 1 || error == 2 || i < min || i > max){
if (fgets(buffer, sizeof buffer, stdin) != NULL) {
buffer[strlen(buffer) - 1 ] = '\0';
if (Validate(buffer) == 0){
i = atoi(buffer);
error = 0;
if ( i < min || i > max){
printf(CURSOR);
}
}
else{
error = 2;
printf(CURSOR);
}
}
else{
error = 1;
printf(CURSOR);
}
}
return i;
}
/***********************************
* Validate
* Parameters:
* char *a: is a pointer to the string containing the possible number
* Returns: int error code (o for good number and 1 for bad number)
* Prints: nothing
***********************************/
int Validate(char *a){
unsigned x;
for (x = 0; x < strlen ( a ); x++){
if (!isdigit(a[x])) return 1;
}
return 0;
}
/*
* OUTPUT
Enter a number for UserInput (0-20)
>>>a
>>>a1
>>>-1
>>>22
>>>12d
Enter number for UserInputFloat (0.00-20.00)
>>>-1.0
>>>0.003
>>>21.5
>>>a12.5
>>>1a2.5
Enter a number for UserInputStrgNumb (0-50)
>>>-1
>>>51
>>>a51
>>>5a1
>>>51a
>>>25a
>>>a25
>>>2a5
>>>25
The number you entered for UserInput = 12
The number you entered for UserInputFloat = 1.00
The number you entered for UserInputStrgNumb = 25
Press [Enter] to close the terminal ...
*/