0

私の息子は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`
4

4 に答える 4

4
int day, month, year;
char t;
std::cin >> day >> t >> month >> t >> year;
于 2013-02-19T20:38:37.943 に答える
1

std正規表現ライブラリを見逃しました!これを行うのに最も安全で効果的な方法だと思います。

トピックに戻ると、は関数なので、を使用してオーバーロードすることはできないと思います(ちなみに禁止する必要があります)。すべての呼び出しの前に追加してみてください。getlineextern "C"using namespace stdstdgetline

于 2013-02-19T20:29:36.150 に答える
1
于 2013-02-19T20:48:33.797 に答える
1

の場合std::istringstream、次のものが必要です。

#include <sstream>

PS使用しないでくださいusing namespace std;。それは悪い習慣であり、最終的にはあなたを困らせるでしょう。

于 2013-02-19T20:37:54.857 に答える