2012-12-07 35 views
1

程序说明: 从“names.txt”中以“First Last”格式读取名称列表。 根据姓氏和名字,根据人名的典型字母顺序对姓名进行排序。 以“Last,First”格式将排序后的列表写入名为“sortednames.txt”的文件中。C++程序的排序名称

这里是我的代码:文件数据存储在全名数组中,但现在我被卡在如何翻转数组中的姓和名?

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; 
} 
+0

确切位置在哪里,你卡住了?你有没有试过在伪代码中写下所需的步骤? –

回答

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); 
} 
+0

它显示我错误spacePosition在这一行。为什么这是?fullname [value] = myString.substr(spacePostion)+“”+ myString.substr(0,spacePostion -1); – user1884679

+0

spacePosition拼写错误,但感谢您的帮助 – user1884679