2012-12-07 33 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