Im trying to learn how to verify an integer from some code Prelude posted a earlier. I can follow most of it but there are a few things Im confused on, first here is his code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
enum ISVAL { VALID, INVALID };
typedef enum ISVAL val_t;
static val_t checknumber ( char *astr )
{
while ( *astr != '\0' )
if ( !isdigit ( *astr++ ) )
return INVALID;
return VALID;
}
int main(void)
{
char astr[10];
if ( fgets ( astr, 10, stdin ) != NULL ) {
if ( astr[strlen(astr)-1] == '\n' )
astr[strlen(astr)-1] = '\0';
if ( checknumber ( astr ) == INVALID )
printf ( "Invalid integer\n" );
else
printf ( "Your number is %d\n", atoi ( astr ) );
}
system("PAUSE");
return 0;
}
Ok as I siad the code I can somewhat follow but the first few lines
Code:
enum ISVAL { VALID, INVALID };
typedef enum ISVAL val_t;
static val_t checknumber ( char *astr )
Thats where I get lost. I have tried looking in up in my book and on the net but I didnt get much.
Can someone please break those three lines down so I might better understand?
Thanks