struct companyInfo{
string companyName; // Name of the Company
string products; // Company's products (number of products in unknown)
};
companyInfo myNewComp;
vector<companyInfo>myVec;
Then When user calls the Add function, I ask for Name of company and ask for its products. Now since I don't know how many products user is going to enter, I use a vector.
1 2 3 4 5 6 7 8 9 10
void Add(){
cout << " Company Name: ";
cin >> myNewComp.companyName; // User can enter "samsung, apple, sony etc."
do{
cout << " Product: "
cin >> myNewComp.products; // User can enter "cellphones, tvs, camera etc."
myVec.push_back(myNewComp);
} while(products != "zzz") // stop loop after user hits "zzz"
}
Now in search function, I ask user for the name of a product and then I want to delete structs (companies) that don't have the product user is looking for.
So if User enters, camera, I would keep struct of Sony and Samsung and Delete Apple.
But I don't Know how to delete entire structs in Vectors.
You can erase an element of a vector using the same code regardless of what type the element is. For example:
1 2 3 4 5 6 7 8
for (int i = 0; i<myVector.size(); i++)
{
if (/* I want to get rid of this*/)
{
myVector.erase(myVector.begin()+i);
i--;
}
}
Also, your code to get all of the products won't work as you expect. Because you only have one string object in the company structure, each time the user inputs a new product it will simply overwrite what they have previously inputted. I think what you want to do is use a vector of strings in the structure itself, then add each product to that vector as the user inputs them. So your struct would become:
1 2 3 4
struct companyInfo{
string companyName; // Name of the Company
vector<string> products; // Company's products (number of products in unknown)
};
@ MODShop,
Thanks but the erase function erases all the products of the company. Even if one product matches, I want it to keep the entire company.
How do I do that? Thanks!