以 mm/dd/yyyy 格式输入日期并拆分为 3 个单独的变量?
Take input of date in format mm/dd/yyyy and split into 3 separate variables?
我正在尝试弄清楚如何进行一些日期验证。我需要能够以 mm/dd/yyyy 的形式从用户那里获取输入,并对其进行各种计算以确定有效性。但是,在我得到输入后,我无法弄清楚如何将日期分成三个变量日、月、年。我玩过 getline 和 get 函数,但我无法弄清楚。感谢新手提前提供的任何帮助。 PS 我不想使用任何内置的日期验证函数。
int main()
{
char fill = '/';
string entered_date;
int entered_month;
int entered_year;
int entered_day;
cout << "Enter a date (mm/dd/yyyy): " << endl;
cin >> entered_date;
//getline(month, 2, '/'); < fix me
//cout << entered_month << "/" << entered_day << "/"
// << entered_year << endl;
system("Pause");
}
在这种情况下您可以使用 scanf,因为它提供的功能比 cin 多得多。
int mm, dd, yyyy;
scanf("%d/%d/%d", &mm, &dd, &yyyy);
这应该可以解决问题。
编辑:另一种方法是以字符串的形式获取整个输入,然后查找子字符串并验证每个部分。
string date;
cin >> date;
string delimiter = "/";
auto start = date.begin(); // iterator at beginning of string
auto finish = date.find(delimiter); // gives position of first occurrence of delimiter
if (finish == date.npos) // find returned the end of string
// delimiter does not exist in string.
else
int month = stoi(date.substr(0, finish)); // Extracts month part from date string
date = date.substr(finish+1); // Removes the month part from the date string
// Understand this piece and write further ahead.
如果您知道您的输入是正确的,那么使用第一部分,因为它会快得多。如果有可能输入不正确,请使用第二个,因为它会更可靠。
最简单的方法是使用std::string::substr
,然后调用stoi
:
#include <string>
#include <iostream>
using namespace std;
int main()
{
char fill = '/';
string entered_date;
int entered_month;
int entered_year;
int entered_day;
cout << "Enter a date (mm/dd/yyyy): " << endl;
cin >> entered_date;
entered_month = stoi(entered_date.substr(0,2));
entered_day = stoi(entered_date.substr(3,2));
entered_year = stoi(entered_date.substr(6));
}
我正在尝试弄清楚如何进行一些日期验证。我需要能够以 mm/dd/yyyy 的形式从用户那里获取输入,并对其进行各种计算以确定有效性。但是,在我得到输入后,我无法弄清楚如何将日期分成三个变量日、月、年。我玩过 getline 和 get 函数,但我无法弄清楚。感谢新手提前提供的任何帮助。 PS 我不想使用任何内置的日期验证函数。
int main()
{
char fill = '/';
string entered_date;
int entered_month;
int entered_year;
int entered_day;
cout << "Enter a date (mm/dd/yyyy): " << endl;
cin >> entered_date;
//getline(month, 2, '/'); < fix me
//cout << entered_month << "/" << entered_day << "/"
// << entered_year << endl;
system("Pause");
}
在这种情况下您可以使用 scanf,因为它提供的功能比 cin 多得多。
int mm, dd, yyyy;
scanf("%d/%d/%d", &mm, &dd, &yyyy);
这应该可以解决问题。
编辑:另一种方法是以字符串的形式获取整个输入,然后查找子字符串并验证每个部分。
string date;
cin >> date;
string delimiter = "/";
auto start = date.begin(); // iterator at beginning of string
auto finish = date.find(delimiter); // gives position of first occurrence of delimiter
if (finish == date.npos) // find returned the end of string
// delimiter does not exist in string.
else
int month = stoi(date.substr(0, finish)); // Extracts month part from date string
date = date.substr(finish+1); // Removes the month part from the date string
// Understand this piece and write further ahead.
如果您知道您的输入是正确的,那么使用第一部分,因为它会快得多。如果有可能输入不正确,请使用第二个,因为它会更可靠。
最简单的方法是使用std::string::substr
,然后调用stoi
:
#include <string>
#include <iostream>
using namespace std;
int main()
{
char fill = '/';
string entered_date;
int entered_month;
int entered_year;
int entered_day;
cout << "Enter a date (mm/dd/yyyy): " << endl;
cin >> entered_date;
entered_month = stoi(entered_date.substr(0,2));
entered_day = stoi(entered_date.substr(3,2));
entered_year = stoi(entered_date.substr(6));
}