我的線性搜索函數僅返回數據中的第一項,而不是搜索項。我是C++的新生,我禁止使用字符串庫,任何人都可以告訴我我做錯了什麼?我遇到了線性搜索功能問題C++
//函數本身....
bool searchDB(const char key[], const Course list[], int size, int (matches) [MAX_CAP], int& matchesSize)
{
bool found = false;
int index;
matchesSize= 0;
for (index = 0; index < size; index++)
{
if (strcmp(key, list[index].name) == 0)
{
matches[matchesSize] = index;
matchesSize++;
found = true;
}
}
return found;
}
//它被稱爲......
void executeCmd(char cmd, Course list[], int& size)
{
Course course;
switch (cmd)
{
case 's':
int i;
char name[MAX_CHAR];
int matches[MAX_CAP];
int matchesSize;
cout << "Please enter the name of the Course you want to search: ";
getString(name, MAX_CHAR);
if (searchDB(name, list, size, matches, matchesSize))
{
for (i = 0; i < matchesSize; i++)
cout << "Match found: " << list[i].name << '\t' << list[i].task << '\t' << list[i].date << endl;
}
else
{
cout << "No match found!" << endl;
}
break;