2014-07-15 2 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 배열의 특정 인덱스를 가진 항목을 반환하는 list [matches [i]]를보고 싶다고 생각합니다.

0

고맙습니다. 그 말이 맞습니다. 틀린 배열을 인쇄하고있었습니다. 내 인쇄 라인이

< < 목록과 같이 있었어야 [경기는 [내가]]. < < '\ t'< < 목록 [경기 [내가]. 작업 < < '\ t'< < 목록을 [이름 [i]와 일치 함. 날짜 < < endl;

관련 문제