2013-05-28 37 views
-3

我正在使用命令行界面編寫一個小程序。在這個程序中,我想擁有一個我設法創建的搜索功能。如何在此C++程序中創建搜索功能

但是,它的字符搜索的名字,但我想創建相同的功能,將能夠使用「學生註冊號碼」進行搜索。

在那裏我遇到了問題搜索案例:

int kWord; 
stdDetails stdFind; 
cout<<"Enter the Student Registration Number of the Student: "; 
cin>>kWord; 
for(int x=0;x<i;x++){ 
    stdFind = stdDetailsStructs_0[x]; 
    if(!strcmp(kWord,stdFind.stdNum)){ 
    search=1; 
    break; 
    } 
} 
if(search==1){ 
    display(stdFind); 
}else{ 
    cout<<"Student details not found please try again."<<endl; 
} 
+0

這是一個很大的代碼要經過.. – JBL

+0

即使你設置了5000的賞金,我也不會讀那個代碼。順便說一句,嘗試使用STL。它可能會幫助你。 – user1764961

+0

好吧我只是編輯它,並把搜索部分只有 –

回答

0

如果它只是一個數字,你爲什麼不能使用==操盤STRCMP。

+0

是的,你是正確的,這是我要去的空白不能介意如何做到這一點我需要使用==,但不知道如何能幫助請。 –

+1

'if(kWord == stdFind.stdNum)''而不是'if(!strcmp(kWord,stdFind.stdNum))' –

+0

並且在其他部分也會使'search = 0' –

0

use --- if(kWord == stdFind.stdNum),,,, strcmp()用於字符串比較。 kWord和stdNum是整數值。

1

我不認爲kWord應該是int,因爲學生註冊號碼應該是字符串。如果它們是數字,則應使用==來檢查相等性。

search宣佈在哪裏?

如果它是一個全球性的,你應該有

if(search==1){ 
    display(stdFind); 
}else{ 
    cout<<"Student details not found please try again."<<endl; 
    search = 0; // <-- add this 
} 
+0

有關學生註冊號碼的好處,請稍微擴展一下,爲什麼他們應該是字符串,即使它的名字中有「數字」。 –

+0

學生註冊號碼應爲字符串類型,以避免對其進行任何意外算術。 – prk

+0

在決定這些事情時,我們應該考慮'數據類型'而不是數據類型 – prk

0

我會用STRNCMP()(即使它真的空調風格,您可以使用STL),因爲它是probally沒有因爲所有的時間'\ n'在行尾。

如果stdNum實際上是一個字符串:

if(!strncmp(kWord,stdFind.stdNum, strlen(stdFind.stdNum))){ 
1

stdNum是int類型的結構stdDetails.So使用==操作insted的的strcmp()

    int kWord; 

        cout<<"Enter the Student Registration Number of the Student: "; 
        cin>>kWord; 
         for(int x=0;x<i;x++){ 

         if(kWord==stdDetailsStructs_0[x].stdNum)      
         { 
          search=1; 
          break; 
         } 
         } 
         if(search==1){ 
          display(stdFind); 
         }else{ 
          cout<<"Student details not found please try again."<<endl; 
         }