ユーザから日付を入力するとき、数値がmm/dd/yyかm/d/yyのいずれであるかを抽出できます。 。私はどちらかをどうやって行うのかしか分かりませんが、両方はできません。これについてどうすればいいですか?私はPythonから来ているC++には新しいです。ここに私のコードです。C++で文字列を解析し、一桁と二桁の両方の日付書式を受け入れる
#include <iostream>
#include <string>
using namespace std;
bool isMagicDate(string year, string month, string day)
{
int int_month = stoi(month);
int int_day = stoi(day);
int int_year = stoi(year);
if (int_month * int_day == int_year)
return true;
else return false;
}
int main()
{
string date;
cout << "enter a date in the format mm/dd/yy: " << endl;
cin >> date;
string month = date.substr(0,2);
string day = date.substr(3,2);
string year = date.substr(6,2);
cout << "the month is " << month << endl
<< "the day is " << day << endl
<< "the year is " << year << endl;
cout << "the date you entered is " << date << endl;
bool magic = isMagicDate(year, month, day);
cout << "Is the date magic? " << magic << endl;
return 0;
}
findとsubstrを使用する区切り文字関数を使用できます。そうすれば、両方の日付形式を扱うことができます。例が必要な場合はお知らせください。しかしここにはありますhttp://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c – geekonedge