I have an array that keeps track of the results of a search of three parallel arrays. The parallel arrays are names, id#s, and balances. A name is associated with an id and balance because they all have the same index. The user searches for names and the program is supposed to output the search results to a text file with the name, id, and balance. So right now every time a search is successful (the name is found in the array), I add the index of that name to an array called resultsAr, like this:
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
I can't figure out how to output this though so it only outputs the names that were found. Right now I just have it doing this:
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;
}
But obviously this just outputs the whole array. I've tried a few th开发者_C百科ings, but I can't figure out what I would do so it would only output the ones that were in resultsAr.
Thanks, and this is homework, so no exact answers, that would be just terrible.
EDIT: The capitalization problem isn't really a problem, I just accidentally did that when I was posting on here I guess, sorry about that. The resultsAr part is working because after searching the contents of the array is the indexes of the names I searched for. The SearchArray() function looks like this:
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;
}
}
My pleasure. Now i see what you are doing. What you have to do is use another indirection. You want to output the result only for those indices which are stored in resultsAr.
Change your for-loop to something similar to this:
int numFound = index;
for(index = 0; index < numFound; index++) {
cout << left << " "<<idsAr[resultsAr[index]];
}
That means, first store the number of indices you found (in the while-loop above this) into "numFound". Then only iterate over 0...numFound-1, and use a double indirection when accessing the element; that means look into resultsAr, which contains the index-found and then use that index to find the actual data.
Does your SearchArray() function return the first index at which a matching string was found in an array of pointers to strings? And then store it in an array which will only have one entry? Even if so the element you are storing is "SearchResult" (capitalized) which is never defined.
-> Please post the complete source-code (including SearchArray()).
EDIT:
Ok, thanks for posting SearchArray(), but we still need more, in the first box you write:
resultsAr[index] = searchResult;
... but without giving us a loop. Also, if you want to find multiple names that match "searchTerm" you will have to write SearchArray() either returning an array-of-indices or accepting a start-index otherwise it will return the first-found name multiple times.
精彩评论