2017-04-20 63 views
1

我的程序允許我添加儘可能多的項目,直到你達到最大值,當你顯示所有你輸入的信息時。當你去顯示一個,當我搜索陣列中的第一個點時,彈出,但在我搜索到我輸入的第4或第5個項目後,它說它沒有找到,但很明顯在那裏。任何幫助。爲什麼我的程序不能在數組中搜索?

#include <stdio.h> 
#include <stdlib.h> 
#define MAX 12 


//Structed Items 

    struct item{ 

    char itemname[20]; 
    char itemdes[30]; 
    int itemID; 
    int itemOH; 
    double itemUP; 
    }; 


// Function Declarations 

    int getMenu_Choice(); 
    int process (int choice, int count, struct item inven[]); 
     int add (int count, struct item inven[]); 
    int showall(int count, struct item inven[]); 
    int find(int count, struct item inven[]); 


int main (void) 
{ // OPENS MAIN 


// Declarations 

    int choice; 
    struct item inven[MAX]; 
     int count = 0; 


// Statements 

do// 
{ 
    choice = getMenu_Choice(); 
    count = process (choice, count, inven); 
} 
while (choice != 0); 


return 0; 


} // CLOSE MAIN 

/*============================getChoice=*/ 

int getMenu_Choice (void) 
{ //OPEN GETCHOICE 


// Declarations 
    int choice; 

// Statements 

    printf("\n\n**********************************"); 
    printf("\n    MENU    "); 
    printf("\n\t1.Create A New Item   "); 
     printf("\n\t2.View All Items   "); 
    printf("\n\t3.View One Item   "); 
    printf("\n\t0.Exit     "); 
    printf("\n**********************************"); 
    printf("\nPlease Type Your Choice Using 0-3"); 
    printf("\nThen Hit Enter: "); 
    scanf("%d", &choice); 

return choice; 

} //CLOSES GET CHOICE 




/*============================process=*/ 

int process (int choice, int count, struct item inven[]) 
{// OPEN PROCESS 


// Declarations 


// Statements 
    switch(choice) 
     { 
      case 1: count = add(count, inven); 
       break; 
      case 2: showall(count, inven); 
       break; 
      case 3: find(count, inven); 
       break; 
      case 0: exit; 
       break; 
      deafult: printf("Sorry Option Not Offered"); 
       break; 

} // switch 

return count; 

} // CLOSE PROCESS 


/*============================add one=*/ 
int add(int count, struct item inven[]) 

{//OPENS CREATE 

// Declarations 

    int i; 


i = count; 

if (i < MAX) 
{ 
    printf("Enter the Item ID:\n"); 
    scanf("%d", &inven[i].itemID); 

    printf("Enter the Item Name:\n"); 
    scanf("%s", inven[i].itemname); 
i++; 

    } 

else { 
    printf("sorry there is no more room for you to add"); 

}; 

return i; 


}; // CLOSE CREATE 



/*============================showall=*/ 

int showall(int count, struct item inven[]) 
{ 
//Declarations 
    int i; 

// Statements 




    for(i = 0; i < count; i++) 
    { 
    printf("\nItem ID : %d", inven[i].itemID); 
    printf("\nItem Name : %s", inven[i].itemname); 
    };  

return 0; 
}; 

/*============================find one=*/ 

int find(int count, struct item inven[]) 
{ 

//Declarations 

    int i; 
    int search; 
    int found; 

    printf("Enter the Item ID to search\n"); 
    scanf("%d", &search); 
    for (i = 0; i < count; i++) 
    { 

    if(inven[i].itemID == search) 

     { 
      printf("\nItem ID : %d", inven[i].itemID); 
      printf("\nItem Name : %s", inven[i].itemname); 
      break; 
} 
    else { 
    printf("\nSorry None existent"); 
    break; 
} 
} 

return 0; 
}; 
+2

調試這些問題的方法是使用調試器單步執行代碼,在每一步檢查狀態。 – kaylum

+0

我從來沒有使用過調試器。我假設這是一個簡單的解決方案,並不需要 –

+1

如果您通過代碼檢查無法看到問題,則無論最終修復是否「簡單」,都需要調試器。對於每個小問題都無法轉向使用Stackoverflow,因此最好學習如何使用調試器 - 這將花費時間。 – kaylum

回答

2

這部分是錯誤的:

所有的
for (i = 0; i < count; i++) 
    { 

    if(inven[i].itemID == search) 

     { 
      printf("\nItem ID : %d", inven[i].itemID); 
      printf("\nItem Name : %s", inven[i].itemname); 
      break; 
} 
    else { 
    printf("\nSorry None existent"); 
    break; 
} 
} 

首先,讓我們修正縮進:

for (i = 0; i < count; i++) 
{ 
    if(inven[i].itemID == search) 
    { 
     printf("\nItem ID : %d", inven[i].itemID); 
     printf("\nItem Name : %s", inven[i].itemname); 
     break; 
    } 
    else 
    { 
     printf("\nSorry None existent"); 
     break; 
    } 
} 

而現在,讓我們思考。此代碼有兩個break s,一個在if中,一個在else中。這是不對的,因爲它在第一次迭代時就打破了循環。因此,執行for循環,如果條件爲真,則執行if中的代碼,如果條件爲假,則執行else中的代碼。但執行將在第一次迭代本身中爆發循環,因爲ifelse都包含break

所以,簡而言之,你的問題是你只是檢查數組的第一個元素。

你可能想

for (i = 0; i < count; i++) 
{ 
    if(inven[i].itemID == search) /* break only if condition is true. Otherwise continue looping */ 
    { 
     printf("\nItem ID : %d", inven[i].itemID); 
     printf("\nItem Name : %s", inven[i].itemname); 
     break; 
    } 
} 

if(i == count) /* Will be true if the loop executed without executing the break */ 
{ 
    printf("\nSorry None existent"); 
} 
+0

是的我的問題仍然沒有解決。 –

+0

當我去搜索我輸入的第二個或第三個項目時,它告訴我它不在那裏,但是如果您在視圖中查看它,它就會顯示出來。 –

+0

這告訴我你沒有正確更新下一期的'search'? –

相關問題