/* PROGRAM:  01_Input.c 
   AUTHOR:      DATE:     Fall 2015
   TOPIC:    Basic C 
   PURPOSE:  01_Lab CST8234
   NOTES:   
             
*/

/**************************************************************************/
/* Declare include files
 **************************************************************************/
#include <stdio.h>
#include <stdlib.h>

/**************************************************************************/
/* Macro Defines
 **************************************************************************/
#define MIN 10
#define MAX 100
#define EFORMAT 1
#define ECHARS 2
#define ERANGE 3

/**************************************************************************/
/* Function prototypes
 **************************************************************************/
int intGet( int , int );
int error( void );



/**************************************************************************/
/* Global variables
 **************************************************************************/

int errorno = 0;

/**************************************************************************
 * Main
 *    Reads a number in between MIN and MAX from the keyboard
 *    Reports errors encountered 
 *    Returns 0 on success
 *            1 on failure
 **************************************************************************/
int main( void ) {
	int number;
printf("Enter a number in between [10-100]:  ");


number=intGet( MIN, MAX );
if(errorno != 0 ) error();
	else printf("Read %d\n ",number);

	return EXIT_SUCCESS;
}

/**************************************************************************
 * intGet
 *      Read an integer number in between min and max
 *      Return: 
 *           number read
 *      Sets global variable errorno
 *           EFORMAT:  Incorrect format ( incorrect starts of characters )
 *           ECHARS :  Extra chars after the number
 *           ERANGE :  Number out of [ MIN, MAX ] range
 **************************************************************************/
int intGet( int min, int max ) {
int userInput;
int input;
char ch;
input = scanf("%d%c",&userInput, &ch );


if ( input==0 ){
errorno = EFORMAT;}

else if ( ch != '\n' ){
errorno = ECHARS;}


else if (userInput < 10 || userInput > 100){
errorno = ERANGE;}




return userInput;


	}
/**************************************************************************/
/* error( )
 *      Displays error message depening of errorno global variable
 *      Terminates with failure
 **************************************************************************/
int error( void ) {
if( errorno == EFORMAT ){
printf("Incorrect input format\n");}

else if( errorno == ECHARS ){
printf("Extra characters\n");}

else if( errorno == ERANGE ){
printf("Input number out of range\n");}
exit( EXIT_FAILURE );
}
