我的程序允許我添加儘可能多的項目,直到你達到最大值,當你顯示所有你輸入的信息時。當你去顯示一個,當我搜索陣列中的第一個點時,彈出,但在我搜索到我輸入的第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;
};
調試這些問題的方法是使用調試器單步執行代碼,在每一步檢查狀態。 – kaylum
我從來沒有使用過調試器。我假設這是一個簡單的解決方案,並不需要 –
如果您通過代碼檢查無法看到問題,則無論最終修復是否「簡單」,都需要調試器。對於每個小問題都無法轉向使用Stackoverflow,因此最好學習如何使用調試器 - 這將花費時間。 – kaylum