fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4. #include <exception>
  5. using namespace std;
  6.  
  7. void parseDate(const string& date, int& year, int& month, int& day)
  8. {
  9. regex dateValidateRe(R"(^(\d{4})\-(\d{1,2})\-(\d{1,2})$)");
  10. smatch matches;
  11. if (!regex_search(date, matches, dateValidateRe))
  12. {
  13. throw invalid_argument("Date format is incorrect");
  14. }
  15. year = stoi(matches[1]);
  16. month = stoi(matches[2]);
  17. day = stoi(matches[3]);
  18. }
  19.  
  20. int main() {
  21. int year, month, day;
  22. string date;
  23. cin >> date;
  24. try
  25. {
  26. parseDate(date, year, month, day);
  27. }
  28. catch (std::exception& ex)
  29. {
  30. cout << "Invalid input: " << ex.what() << endl;
  31. }
  32. cout << "The date entered was Year = " << year << " Month = " << month << " Day = " << day << endl;
  33. return 0;
  34. }
Success #stdin #stdout 0s 4240KB
stdin
2018-4-8
stdout
The date entered was Year = 2018 Month = 4 Day = 8