So I have a struct called CompanyInfo.
1 2 3 4 5 6
|
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 in a New Vector, I save information of a company whose product matches the product user is looking for.
So if User is looking for camera, in my new vector, I save Samsung and Sony (since Apple doesn't sell cameras)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
companyInfo usersChoice;
vector<companyInfo>copiedVec;
void Search()
{
string productLookingfor;
cout << " Which product are you looking for? "
cin >> productLookingfor;
for (int i=0; i<myVec.size(); i++) // go through the vector
{
if (productLookingfor == myVec[i].products) // if the product is what you're looking for
{
usersChoice.companyName = myVec[i].companyName; // copy name of company in new struct
for (int i=0; i<myVec.size(); i++) // then copy all the products
{
usersChoice.product = myVec[i].product; // THE PROBLEM IS PROBABLY HERE
copiedVec.push_back(usersChoice);
}
}
}
}
|
// FOR SOME REASON IT IS COPYING ALL THE PRODUCTS, EVEN THE PRODUCTS OF COMPANEIS THAT USER DOESN'T WANT.
// IF USER IS LOOKING FOR CAMERA, THEN I WANT IT TO COPY ONLY "SAMSUNG" AND "SONY" BUT THE SECOND 'for' LOOP IN SEARCH FUNCTION COPIES ALL THE PRODUCTS.
HOW DO I FIX THIS ????