我有一個數組可以跟蹤三個並行陣列的搜索結果。平行數組是名稱,ID號和餘額。名稱與ID和平衡相關聯,因爲它們都具有相同的索引。用戶搜索名稱,程序應該將搜索結果輸出到名稱,ID和餘額的文本文件。所以現在每一個搜索成功時(該名稱在數組中),我那個名字的索引添加到一個名爲resultsAr一個數組,像這樣:基於另一個陣列的內容輸出三個陣列到文件
while(searchTerm != "done")
{
searchResult = SearchArray(searchTerm, AR_SIZE, namesAr);
if(searchResult == -1)
{
cout << searchTerm << " was not found.\n";
}
else
{
cout << "Found.\n";
resultsAr[index] = searchResult;
index++;
}
cout << "\nWho do you want to search for (enter done to exit)? ";
getline (cin,searchTerm);
} // End while loop
我無法弄清楚如何輸出這個,所以它只輸出找到的名字。現在我只是這樣做:
outFile << fixed << setprecision(2) << left;
outFile << setw (12) << "Id#" << setw(22) << "Name" << "Balance Due"
<< endl << endl;
for(index = 0; index < sizes; index++)
{
outFile << left << setw (10) << idsAr[index] << setw(22) << namesAr[index]
<< setw(3) << "$";
outFile << right << setw(10) << balancesAr[index] << endl;
}
但顯然這只是輸出整個數組。我已經嘗試了一些東西,但我無法弄清楚我會做什麼,因此它只會輸出resultsAr中的那些東西。
謝謝,這是家庭作業,所以沒有確切的答案,這將是可怕的。
編輯:大寫問題並不是一個真正的問題,我只是不小心做到了這一點,當我在這裏張貼我想,對不起。 resultsAr部分正在工作,因爲在搜索數組的內容之後是我搜索的名稱的索引。該SearchArray()函數如下:
int SearchArray(string searchTerm,
int size,
string namesAr[])
{
// Variables
int index;
bool found;
// Initialize
index = 0;
found = false;
while(index < size && !found)
{
if(namesAr[index] == searchTerm)
{
found = true;
}
else
{
index++;
}
}
if(found)
{
return index;
}
else
{
return -1;
}
}
非常感謝,這非常有意義。你真棒。 – Michael 2011-02-24 11:31:06