I am trying to delete the array element that matches with the user input. So I am trying to shift all the cells after the deleted one to the left 1 spot. I know it's still in the loop of the if statement(line 11 and 31) but I can't figure out how else to do it.

//Can't figure this out
void deleteID(inventory& inv)
{
     char id[8];
     cout << "Enter search value: "; cin >> id;
     
     for (int index = 0; index < inv.total; index++)
     {
         if ((strcmp(inv.item[index].id, id)) == 0)
         {
          inv.item[index ] = inv.item[index + 1];
         }
         
     }
     
     inv.total -= 1;

}

//Can't figure this out
void deleteName(inventory& inv)
{
     
     char name[20];
     cout << "Enter search value: "; cin >> name;
     
     for (int index = 0; index < 9; index++)
     {
         if ((strcmp( inv.item[index].productName, name)) == 0)
            {
             inv.item[index] = inv.item[index + 1];    
            }
     }
     
     inv.total -= 1;
}

Why not use a list of strings rather than a character array? The list class is made for fast insertion and deletion, without you having to worry about shifting values continually.

Why not use a list of strings rather than a character array? The list class is made for fast insertion and deletion, without you having to worry about shifting values continually.

We are not allowe to use classes yet.

We are not allowe to use classes yet.

You are using iostream ;)

All joking aside, all your loop does is copy the next value to the position you planned on deleting, but that just makes two copies of the next position's data. The tedious way would be to use two loops. Once you've found the value to delete, start the next loop and copy the next value to the current position.

Also, why is your for loop checking for index < 9? Should it be inv.total?

You are using iostream ;)

All joking aside, all your loop does is copy the next value to the position you planned on deleting, but that just makes two copies of the next position's data. The tedious way would be to use two loops. Once you've found the value to delete, start the next loop and copy the next value to the current position.

Also, why is your for loop checking for index < 9? Should it be inv.total?

I put 9 there to test something but it's really supposed to be (index < inv.total).
inv.total is the total number of elements in the array or the total number of inventory.

Ive finally figured out how to do it I had to put another for loop inside of the if loop. Thanks for your help Chilton.

void deleteID(inventory& inv)
{
     char id[8];
     cout << "Enter search value: "; cin >> id;
     
      for (int index = 0; index < inv.total; index++)
     {
            if ((strcmp( inv.item[index].id, id)) == 0)
           {
           
            for (int g = index; g < inv.total; g++)
                {
                     inv.item[index] = inv.item[++index];
                     }
                inv.total -= 1;
           
           }
     }
}

void deleteName(inventory& inv)
{
     
     char name[20];
     cout << "Enter search value: "; cin >> name;
     
     for (int index = 0; index < inv.total; index++)
     {
            if ((strcmp( inv.item[index].productName, name)) == 0)
           {
           
            for (int g = index; g < inv.total; g++)
                {
                     inv.item[index] = inv.item[++index];
                     }
                inv.total -= 1;
           
           }
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.