Reputation: 11
If user enters date in mm / dd / yyyy
format, how do I retrieve it correctly?
std::cin >> month >> day >> year;
does not work.
Upvotes: 1
Views: 623
Reputation: 596156
Have a look at std::get_time()
, eg:
#include <iomanip>
#include <ctime>
std::tm tmb;
std::cin >> std::get_time(&tmb, "%m/%d/%Y");
int month = tmb.tm_mon + 1;
int day = tmb.tm_mday;
int year = tmb.tm_year + 1900;
Upvotes: 2
Reputation: 73
First of all, you can define these variables to save the values:
int month;
int day;
int year;
char dummy;
Then you have two options:
cin >> month >> dummy >> day >> dummy >> year;
cin >> month;
if (cin.get() != '/')
{
cout << "Follow the format month/day/year" << endl;
return 1;
}
cin >> day;
if (cin.get() != '/')
{
cout << "Follow the format month/day/year" << endl;
return 1;
}
cin >> year;
Upvotes: 0