2014-07-15 25 views
0

我的線性搜索函數僅返回數據中的第一項,而不是搜索項。我是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; 

回答

0

我認爲這個問題是你是什麼樣的打印而不是你所發現的。

list [i]只會返回位置i中列表中的所有內容。我認爲你要查看列表[matches [i]],你要返回items和matches數組中的特定索引。

0

謝謝,這是正確的,我打印出錯誤的數組。我的打印線應該是這樣的

< <列表[匹配[I]。名稱< < '\ T' < <列表[匹配[I]。任務< < '\ T' < <名單[匹配[i]]。date < < endl;