Thread: DATE <TIME.H> data validation

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    DATE <TIME.H> data validation

    Hi guys i have a major problem.

    Could someone help me with some data validation input.

    Basically i need to be able to input 10 characters usings fgets() not scanf(). And check for a date validation.

    So for example

    dd/mm/yy it must be in order of date validation.
    Could someone please help me

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well I imagine it would start something like this
    Code:
    #include <stdio.h>
    
    int main ( ) {
      char buff[BUFSIZ];
      if ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
        int y,m,d;
        if ( sscanf( buff, "%d/%d/%d", &d, &m, &y ) == 3 ) {
          /* got 3 ints */
          if ( d >= 1 && d <= 31 ) /* etc etc */
        }
      }
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    would it also check the forward slash '/' for the date

    Hi thanks for that reply thats handy.. Would that also check for the / character when inputted into the standard in?

  4. #4
    FOX
    Join Date
    May 2005
    Posts
    188
    Quote Originally Posted by bazzano
    Hi thanks for that reply thats handy.. Would that also check for the / character when inputted into the standard in?
    Yes, that's what "%d/%d/%d" in sscanf does. It looks for input of the format: int/int/int, so 100/1000/10000 will still get through, but as Salem showed, it can easily be detected by comparing the stored values against a valid range.

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    cool thanks for what

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  4. Dynamic data members?
    By confusalot in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:15 AM
  5. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM