1

程序说明:从“names.txt”中以“First Last”格式读取姓名列表。根据姓氏然后名字的人名的典型字母顺序对名称进行排序。将排序列表写入名为“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 回答