私の息子はC++を学んでおり、彼の演習の1つは、ユーザーに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`