i have a program that would display the productName,productPrice,productCode.

each is a separate array.

the program would prompt user to add,delete,or search a productName,productPrice,productCode.

here's what i tried to do:

string productName[100]={"milk","beef"};
int productPrice[100]={10,11};
string productCode[100]={"a1","a2"};
int x,y,z;
char choice;
string search,name,price,code;

cout<<"what do you want to do?";
    <<"[S]earch.";
    <<"[A]dd.";
    <<"[D]elete.";
cin>>choice;

if (choice=="s"||choice=="S")
{
   cout<<"enter product name: ";
   cin>>search;
   for(x=0,x<2,x++)
   {
    cout>>productName[x];
   }
}

if (choice=="a"||choice=="A")
{
   cout<<"enter product details: ";
   cin>>name>>price>>code;
   // i dont know the code here (add)
}

if (choice=="d"||choice="D")
{
   cout<<"enter product name: ";
   cin>>name;
   //i dont know the code here (delete)
}

i appreciate any help.

Edited 5 Years Ago by Nick Evan: Added CODE tags.

First of all, you should save the number of elements in a variable, e.g. int productNumber.
Each time you add a product, you should increase this variable and you should decrease it in case of deleting products.

So when you want to add a new element, you should do as follows:

productName[productNumber] = name;
productPrice[productNumber] = price;
productCode[productNumber++] = code;

For deleting a ith element of the array, you should just shift all next elements ((i + 1)th to last) one place to left (e.g. productNumber = productNumber, and so on) and then decrease the productNumber;

can you give me a snippet of shifting elements. or a part of your own version. that would be great.

Assume that you are going to delete ith element:

for (int j = i; j < productNumber - 1; j++) {
   productName[j] = productName[j + 1];
   // Same for two other arrays
}

productNumber--;
This article has been dead for over six months. Start a new discussion instead.