【发布时间】:2013-02-19 20:25:50
【问题描述】:
我儿子正在学习 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`
【问题讨论】:
-
你帮孩子做事并没有帮助他。
-
感谢 Austin,但他也坐在我旁边查看文档以解决这个问题。所以我称之为团队合作
-
我可以补充一下,这是他放学的一周,所以也是课外活动。
-
size_t getline(char**, size_t*, FILE*)是POSIX function。我不认为试图称呼它是你的意图,或者是吗? -
@AustinMullins 和孩子们一起工作是一种极大的乐趣。