PLEASE HELP::COPY SPECIFIC ELEMENTS FROM A VECTOR (OF STRUCTS)

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 ????


Last edited on
// FOR SOME REASON IT IS COPYING ALL THE PRODUCTS

It does more than that. For every item in myVec that has correct product, you add whole myVec to copiedVec.
1
2
3
4
5
6
7
for ( const auto & x : myVec )
{
  if ( productLookingfor == x.products )
  {
    copiedVec.push_back( x );
  }
}
// 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 ????

don't hold caps locked is a good start

1
2
3
4
struct companyInfo{
        string companyName; // Name of the Company
        string products;   // Company's products (number of products in unknown)
};

Your Companies can have only 1 product, maybe you want to make this a vector of std::strings
Hi keskiverto,
I tried doing what you have but I am gettting some erros.

1
2
3
error: ISO C++ forbids declaration of 'X' with no type [-fpermissive]

error: range-based 'for' loops are not allowed in C++98 mode


Can you tell me how to fix this please ?
FYI: I can also erase myVec instead of copying its elements to a new vector. But I don't know how to erase entire structs in myVec.
in C++98 mode

It sounds like your compiler knows about new standard, but does not by default operate in "new mode". Consult the documentation of your compiler.
Topic archived. No new replies allowed.