【发布时间】:2011-02-24 09:21:56
【问题描述】:
我有一个数组来跟踪三个并行数组的搜索结果。并行数组是名称、id#s 和余额。名称与 id 和 balance 相关联,因为它们都具有相同的索引。用户搜索名称,程序应该将搜索结果输出到包含名称、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;
}
}
【问题讨论】: