Preludes: verify int code

This is a discussion on Preludes: verify int code within the C++ Programming forums, part of the General Programming Boards category; Im trying to learn how to verify an integer from some code Prelude posted a earlier. I can follow most ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    Preludes: verify int code

    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

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    this enumerates two values, VALID = 0, INVALID = 1 as type of ISVAL.

    enum ISVAL { VALID, INVALID };

    this typedef in the most basic explaination gives "enum ISVAL" an alias of val_t so val_t can be used anywhere in place of "enum ISVAL" and vice versa

    typedef enum ISVAL val_t;

    this declares a static(explained in a minute), named checknumber that takes a charater string as an reguement and returns a val_t alias to "enum ISVAL".

    the 'static' keyword specifies that this function is not visible outside the file in which it is defined.

    static val_t checknumber ( char *astr )
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    thanks

    could not have been better worded for me to understand.

    Thank you!
    Rye

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  3. code problem
    By kashifk in forum C Programming
    Replies: 6
    Last Post: 04-22-2003, 05:22 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21