我儿子正在学习 C++,他的一个练习是让用户输入 DD/MM/YYY 中的日期,然后将其输出为月日年
So: 19/02/2013
Output: February 19, 2013.
我试图帮助他理解各种方式,但现在我自己也很困惑。
获取行()
std::string::substr()
std::string::find()
std::string::find_first_of()
std::string::find_last_of()
我无法完全正确地解决这些问题。
我目前尝试解析的是:
#include <iostream>
#include <string>
using namespace std;
int main (void)
{
string date;
string line;
cout << "Enter a date in dd/mm/yyyy format: " << endl;
std::getline (std::cin,date);
while (getline(date, line))
{
string day, month, year;
istringstream liness( line );
getline( liness, day, '/' );
getline( liness, month, '/' );
getline( liness, year, '/' );
cout << "Date pieces are: " << day << " " << month << " " << year << endl;
}
}
但我收到如下错误:
`g++ 3_12.cpp -o 3_12`
`3_12.cpp: In function ‘int main()’:`
`3_12.cpp:16: error: cannot convert ‘std::string’ to ‘char**’ for argument ‘1’ to ‘ssize_t getline(char**, size_t*, FILE*)’`
`3_12.cpp:18: error: variable ‘std::istringstream liness’ has initializer but incomplete type`
最佳答案
int day, month, year;
char t;
std::cin >> day >> t >> month >> t >> year;
关于c++ - 在 C++ 中解析日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966710/