简体   繁体   中英

Taking Date input in the form of “yyyy/mm/dd, hh:mm” from user and validate the date in C++?

I'm pretty new in C++ and trying to figure out how to solve this problem, so any help is absolutely appreciated. I need to take Date input from user in the form of "yyyy/mm/dd, hh:mm" in an istarem& function and validate its parts. So far, I have tried everything that I've learned and my best solution with minimum error is the following code. There is also a predefined tester client code which I can't modify. There are many steps in the client code in which every validation gets tested. for example:

one input is "2000/1/50" and I get the DAY_ERROR which is true. another input is "2000/1/1, 25:10" and I get the HOUR_ERROR which is true as well. Everything goes fine until the last part (checking minute). When the input is "2000/1/1, 23:60" I get the HOUR_ERROR again instead of MIN_ERROR. I'm not sure if it's because of the whitespace after "," or not. However, if that's the case I don't know how to fix it.

std::istream& Date::read(std::istream& is = std::cin) {
    int year;
    int mon;
    int day;
    int hour;
    int min;
    bool valid;
    ((((is >> year).ignore(100, '/') >> mon).ignore(100, '/') >> day).ignore(100, ',') >> hour).ignore(100, ':') >> min;

        bool val_year = sizeof(year) == 4 && year >= MIN_YEAR && year <= MAX_YEAR;
        bool val_mon = mon >= 1 && mon <= 12;
        bool val_day = day >= 1 && day <= mday();
        bool val_hour = sizeof(hour) == 2 && hour >= 0 && hour <= 23;
        bool val_min = sizeof(min) == 2 && min >= 0 && min <= 59;
        valid = val_year && val_mon && val_day && val_hour && val_min;
        if (valid) {
            errCode(NO_ERROR);
            Date D2(year, mon, day, hour, min);
        }
        else {
            if (!val_year)
                errCode(YEAR_ERROR);
            else if (!val_mon)
                errCode(MON_ERROR);
            else if (!val_day)
                errCode(DAY_ERROR);
            else if (!val_hour)
                errCode(HOUR_ERROR);
            else if (!val_min)
                errCode(MIN_ERROR);
            else
                errCode(NO_ERROR);
            }
        }
        return is;
    }

Here's one suggestion:

/*
 * EXAMPLE INPUT: 2/22/17, 20:15
 * EXAMPLE OUTPUT: input: 02/22/17, 20:15
 */
#include <stdio.h>

int main()
{
  int year, month, day, hour, min;
  char buff[80];

  fgets (buff, sizeof(buff), stdin);
  int n = sscanf(buff, "%d/%d/%d, %d:%d", &year, &month, &day, &hour, &min);
  if (n != 5) {
    printf ("Error parsing input: expected 5 values, got %d...\n", n);
    return 1;
  }
  printf ("input: %02d/%02d/%d, %02d:%02d\n", year, month, day, hour, min);

  return 0;
}

As far as validating the date: your code looks fine.

Or you might want to consider reading your date/time values into a struct tm_date (instead of year, month, day, etc.), then using strftime to validate.

Here is an example:

taking date as dd/mm/yy in c language

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM