为什么我的程序不会在数组中搜索?

时间:2017-04-20 05:10:54

标签: c

我的程序允许我添加任意数量的项目,直到达到最大值,当你显示所有输入的信息时。当你去显示一个时,当我搜索数组中的第一个点时它弹出,但是在我搜索了我输入的第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;
};

1 个答案:

答案 0 :(得分: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");
}