2010-09-11 8 views
1

我遇到了一個問題,在特定的函數中使用stricmp,在其他函數中它完美地工作,除了這一個。 問題是即使它比較相同的字符串(char *)也不會返回0. 可能是什麼問題? (抱歉的混亂,我會嘗試進行格式化) 是這樣的代碼:stricmp不起作用

Employee* CityCouncil::FindEmp(list<Employee*> *lst, char* id) 
{ 
bool continue1 = true; 
Employee* tmp= NULL; 
char* tmpId = NULL, *tmpId2 = NULL; 
list<Employee*>::iterator iter = lst->begin(); 
if ((id == NULL) || (lst->empty()==true)) 
    return NULL; 
while ((iter != lst->end()) && (continue1)){ 
    tmp = (Employee*)(*iter); 
    tmpId = (*tmp).GetId(); 
    if(tmpId != NULL) 
    { 
     if(stricmp(tmpId,id) == 0) 
     continue1 = false; 
    } 
    if (continue1 == true) 
    iter++; 
} 
if (iter == lst->end()) 
    return NULL; 
return (Employee*)(*iter); 
} 
+0

請在發佈問題時使用預覽。這次更正了源碼格式。 – AndiDog 2010-09-11 17:21:03

+0

請用語言(C/C++?)標記問題。 Visual Studio支持多種語言,因此您需要更具體。 – Oded 2010-09-11 17:23:56

+0

根據MSDN - http://msdn.microsoft.com/en-us/library/ms235365(v=VS.100).aspx - 'stricmp'現已被棄用。您應該使用'_stricmp'來替代 - http://msdn.microsoft.com/zh-cn/library/k59z8dwe.aspx – ChrisF 2010-09-11 17:24:53

回答

4

從不責備屬於C庫函數。 stricmp肯定按預期工作,這意味着字符串真的不同。這個函數中的邏輯必定有問題 - 您應該使用printf語句來找出字符串不同的位置和原因。

編輯:我把一個簡單的測試程序放在一起。這適用於我:

#include <stdio.h> 
#include <list> 
using namespace std; 

// Dummy 
class Employee 
{ 
    public: 
     Employee(const char *n){ id = strdup(n); } 
     char *id; 
     char *GetId() { return this->id; } 
}; 

Employee* FindEmp(list<Employee*> *lst, char* id) 
{ 
    bool continue1 = true; 
    Employee *tmp = NULL; 
    char* tmpId = NULL; 

    list<Employee*>::iterator iter = lst->begin(); 
    if(id == NULL || lst->empty()) 
     return NULL; 

    while(iter != lst->end() && continue1) 
    { 
     tmp = (Employee*)(*iter); 
     tmpId = (*tmp).GetId(); 
     if(tmpId != NULL) 
     { 
      if(stricmp(tmpId,id) == 0) 
       continue1 = false; 
     } 
     if(continue1 == true) 
      iter++; 
    } 

    if(iter == lst->end()) 
     return NULL; 

    return (Employee*)(*iter); 
} 


int main(int argc, char **argv) 
{ 
    list<Employee*> l; 

    l.push_back(new Employee("Dave")); 
    l.push_back(new Employee("Andy")); 
    l.push_back(new Employee("Snoopie")); 

    printf("%s found\n", FindEmp(&l, "dave")->GetId()); 
    printf("%s found\n", FindEmp(&l, "andy")->GetId()); 
    printf("%s found\n", FindEmp(&l, "SnoOpiE")->GetId()); 

    return 0; 
} 

請注意,我使用了您提供的功能。同樣,stricmp沒有問題,問題必須在你的代碼中。

+0

謝謝,這很快,因爲它有幫助:) – 2010-09-11 18:00:52

+0

將工作我的格式和標記! – 2010-09-11 18:01:23

+0

只是爲了確保一個char *字段的getter在字符串的末尾自動添加一個空終止符? – 2010-09-11 18:03:29

1

您的代碼看起來正確;也許你沒有傳遞你認爲自己的字符串,或者內存不知何故被破壞?

但是,您的功能可以寫得更乾淨。

// Why is this a method off of CityCouncil? As written, it 
// doesn't use any CityCouncil members. 
// 
// Also, why not pass lst by reference? Like this: 
// Employee* CityCoucil::FindEmp(list<Employee*>& lst, char *id) { ... 
// 
// Also, const correctness is a good idea, but that's more complicated. 
// Start with this: 
// Employee* CityCoucil::FindEmp(const list<Employee*>& lst, const char *id) { ... 
// 
// Also, it's easy to leak memory when using a list of pointers, but that's 
// another topic. 
Employee* CityCouncil::FindEmp(list<Employee*> *lst, char* id) 
{ 
    // No need for continue1; we'll use an early return instead; 
    // No need for tmp, tmpId, tmpId2; just call methods directly 
    // off of iter. 

    if (id == NULL || lst->empty()) 
     return NULL; 

    // You should NOT do a C-style "(Employee*)" cast on *iter. 
    // C-style casts are generally to be avoided, and in this case, 
    // it shouldn't be necessary. 

    // Use a for loop to simplify your assigning iter and incrementing it. 
    for (list<Employee*>::iterator iter = lst->begin(); iter != list->end(); iter++) { 
     if ((*iter)->GetId()) { 
      if (stricmp((*iter)->GetId(), id) == 0) { 
       return *iter; 
      } 
     } 
    } 
    return NULL; 
} 
+0

現在看起來更好,謝謝 – 2010-09-11 18:07:22