1

My program allows me to add as many items until you hit the max, when you when all it shows you all the information you have entered in. When you go to display one, when I search for the first spot in the array it pops up, but after I search the 4th or 5th item I entered in, it says it hasn't been found, but it's obviously there. Any help.

#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;
};
5
  • 2
    The way to debug such issues is to use a debugger to step through the code, examining the state at each step. Commented Apr 20, 2017 at 5:12
  • I have never used a debugger before. I was assuming that it was an easy fix that didn't require that Commented Apr 20, 2017 at 5:17
  • 1
    If you can't see what the problem is by code inspection then a debugger is required irrespective of whether the final fix is "easy" or not. Can't turn to Stackoverflow for every small problem so best to learn to use the debugger now - it will be time well spent. Commented Apr 20, 2017 at 5:24
  • Always indent your code properly! It'll help you a lot. BTW, those ; at the end of } is uneccessary. And why does showall and find return an int? Why not make the return type void? Commented Apr 20, 2017 at 5:45
  • exit;exit(0);, deafult:default: Commented Apr 20, 2017 at 5:49

1 Answer 1

2

This part is wrong:

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;
}
}

First of all, lets fix the indentation:

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;
    }
}

And now, lets think. This code has two breaks, one in the if and one in the else. This isn't right as it breaks the loop right at the first iteration. So, your for loop executes, and the code in the if executes if the condition is true, and the code in the else executes if the condition is false. But the execution will break out the loop in the first iteration itself as both the if and else contains a break.

So, in short, your problem was that you were just checking the first element of the array.

You probably wanted

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");
}
Sign up to request clarification or add additional context in comments.

2 Comments

when I go to search for the second or third item I entered it tells me its not there however if you view it in view all it shows up.
That tells me you are not updating search properly for the next term?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.