Hello!
TLDR
Figuring out how to access values of objects stored in vectors can be tricky for new programmers. In this case, the solution is to use the arrow operator -> to call the GetName() function.
/TLDR
1) First, let's look at how the ItemToPurchase class currently works.
Each ItemToPurchase object holds data about the item, including the item name which is a string. You already have a GetName() function so let's use it. Example:
1 2 3 4 5
|
// create an Apple object and a pointer to access it
ItemToPurchase * apple = new ItemToPurchase("Green Apple", "A sour green apple", 0.99, 1);
// access the apple's name using the arrow operator
cout << apple->GetName() << endl;
|
You may wonder why we're using the arrow -> operator to access GetName(). That's because the "apple" object we created is a pointer rather than the object itself. This mirrors your ShoppingCart class which stores a vector of pointers.
2) Now we know how to call GetName() to retrieve the item's name by itself. The next step is to use it in a vector of ItemToPurchase pointers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// vector holds items added to shopping cart
vector<ItemToPurchase*> allItemsToPurchase;
// create example objects (ShoppingCart::AddItem() function should do something similar)
ItemToPurchase * apple = new ItemToPurchase("Green Apple", "A sour green apple", 0.99, 1);
ItemToPurchase * orange = new ItemToPurchase("Orange", "It's just an orange", 1.25, 1);
// ...and add them to the vector
allItemsToPurchase.push_back(apple);
allItemsToPurchase.push_back(orange);
// Setup complete. Let's print the name of each item in vector
for (int i = 0; i < allItemsToPurchaset.size(); i++)
{
cout << allItemsToPurchase[i]->GetName() << endl;
}
|
The important part is
allItemsToPurchase[i]->GetName()
. You can read this in English as: Look in our vector at the current index and get the pointer there. It points to the object we want. Then use the arrow operator to access the object's GetName() method.
What does it return? A string. Why does it return a string? Because we said it does in the ItemToPurchase class. Why is that useful? Because now we can compare the itemName with the string in the LocateItem() function:
1 2 3 4 5
|
for (int i = 0; i < itemsToPurchase.size(); i++)
{
if (itemName == itemsToPurchase[i]->GetName())
index = i;
}
|
Please do not add this code directly in to your project. The goal is not to give you the code, but to walk through a simple example to solve of the problem you're having. Considering the level of knowledge your code demonstrates, you should be able to understand every step of the example. If something is unclear, feel free to ask and we can clarify.
P.S.
Does it have to be that way? No, it doesn't. I personally avoid pointers unless the project explicitly requires it. How can we do it differently? By using a vector of objects instead of a vector of pointers. This lets you use the dot operator rather than the arrow operator:
vector<ItemToPurchase>
rather than
vector <ItemToPurchase*>
.
P.P.S.
I mentioned it in your other thread, and I'm going to repeat it here. Don't ask for user input inside ShoppingCart::UpdateQuantity(). You already know what you're looking for thanks to the "string itemName" argument.