1

プログラムの説明:「names.txt」から「FirstLast」の形式で名前のリストを読み取ります。家系の名前、次に名に基づいて、人々の名前の一般的なアルファベット順に基づいて名前を並べ替えます。ソートされたリストを「sortednames.txt」というファイルに「Last、First」の形式で書き込みます。

これが私のコードです:ファイルデータはフルネーム配列に保存されていましたが、配列の名前と名前を反転する方法に固執していますか?

int main()
{
    const int MAXNAMES = 100;
    int value = 0;
    string fullname[MAXNAMES];

    ifstream inFile;
    inFile.open("names.txt"); //open the file to excess the rainfall data
    if (inFile.fail()) // testing the file
        {
            cout << "Error opening file. Please check that the file currently `enter code here`exist" << endl;
            exit(1);
        }   
    cout << "File successfully open" << endl;
    while(!inFile.eof())
    {
        while(value < 100)
        {
            getline(inFile,fullname[value]);
            value++;
        }
    }

    return 0;
}
4

1 に答える 1

0

名前を反転するには、次のようにします。

string myString;
int spacePosition;

value = 0;
while(value < 100) {
   myString = fullname[value];
   spacePosition = myString.find(" ");
   fullname[value] = myString.substr(spacePostion) + " " + myString.substr(0, spacePostion -1);
}
于 2012-12-07T07:37:47.283 に答える