我儿子正在学习 C++,他的一个练习是让用户在 DD/MM/YYY 中输入日期,然后将其输出到月日、年
So: 19/02/2013
Output: February 19, 2013.
我试图帮助他理解各种方式,但现在我自己也搞糊涂了。
getline()
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`