Thread: Error check date loop code

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    15

    Error check date loop code

    Code:
    void getDate(Car_t fleet[], int z, char dateType) /*Function that gets the date from the user*/
    {
        int day, month, year;
        /*put a loop here to error check*/
        if(dateType = 'm')
        printf("Please enter manufacture date in dd/mm/yyyy format> ");
        else if (dateType = 'p')
        printf("Please enter purchase date in dd/mm/yyyy format> ");
        else
        printf("Error, unknown date format\n");
        scanf("%d/%d/%d", &day, &month, &year);
        /*end loop here*/
    I need to use a loop in the there, which will check the dates are in range from 1-31, months are from 1-12, and year from 1900-2012, otherwise it will ask again. Tried a while loop but gets very complicated and then the function does not work anymore. Can someone help please?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I believe a "do-while" loop is what you are after
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    So in the while part i would write?
    Code:
    while(day>0 && day <32, month>0 && month<13, year>1899 && year<2013)

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    Is that correct?

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    is that correct
    No.
    Replace the commas with "&&" should do it.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    I did. It does not work i think, it still accepts dates like 35 .

    Here is the code
    Code:
    void getDate(Car_t fleet[], int z, char dateType) /*Function that gets the date from the user*/{
        int day, month, year;
        do
        {
            if(dateType == 'm')
            printf("Please enter manufacture date in dd/mm/yyyy format> ");
            else if (dateType == 'p')
            printf("Please enter purchase date in dd/mm/yyyy format> ");
            else
            printf("Error, unknown date format\n");
            scanf("%d/%d/%d", &day, &month, &year);
        }while(day>0 && day<32 && month>0 && month<13 && year>1899 && year<2013);

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try entering a valid date. Does it work? Why, or why not?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    It works with both valid and invalid dates now.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > if(dateType = 'm')
    Use ==

    > scanf("%d/%d/%d", &day, &month, &year);
    If the user gets all fat-fingered (or stupid) and types in "hello world", your scanf loop will just lock up and get no further.

    Separate input from conversion.
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
      if ( sscanf(buff,"%d/%d/%d", &day, &month, &year) == 3 ) {
        // now validate the input data
        if ( valid ) break; // success
      } else {
        // error message
      }
    }
    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.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You want to loop only if that statement is NOT true
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        int day, month, year;
        do
        {
            printf(">>");
            scanf("%d/%d/%d", &day, &month, &year);
        }while( !((day>0 && day<32) && (month>0 && month<13) && (year>1899 && year<2013)));
    
    
        printf("%d/%d/%d", day, month, year);
    
    
        return EXIT_SUCCESS;
    }
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Salem View Post
    If the user gets all fat-fingered (or stupid)...
    youtube

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-17-2012, 01:56 PM
  2. Replies: 12
    Last Post: 01-08-2009, 12:15 PM
  3. error can some1 check my code
    By yahoosilver in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 08:14 AM
  4. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  5. error check...loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-01-2001, 10:46 PM