Stuck

How do you add new items to a vector in a header/implementation project? I'm working with headers and implementations. I'm doing a "grocery cart" assignment.

Here's the prompt for the item and the quantity. I need help adding this new
(string) to the vector in another file. It feels simple but I can't grasp.

Vector name: vector<GroceryItem> shoppingCart;
Item function: newItem(Grocery Item a); (header)

void Customer::newItem(GroceryItem a) // function to add new item to cart (implementation)
{
shoppingCart.push_back(a);
}

while ( response == "y" )
{
cout << "Which item will you purchase?" << endl;
cout << "White Rose Low Fat Yogurt (item code = 0)" << endl
"1.99" << endl << endl;

"Diamond Crystal Salt (item code = 1)" << endl;
".89" << endl << endl;

"Trend Detergent (item code = 2)" << endl;
"1.49" << endl << endl;

"Bumble Bee Tuna (item code = 3)" << endl;
"0.89" << endl << endl;

"Carmela Corned Beef (item code = 4)" << endl;
"2.99" << endl << endl;

"Goya Olive Oil (item code = 5)" << endl;
"4.99" << endl << endl;

"Friendly's Ice Cream (item code = 6)" << endl;
"2.99" << endl << endl;

"Bird's Eye Mixed Vegetable (item code = 7)" << endl;
"0.99" << endl << endl;

"Prego Pasta Sauce (item code = 8)" << endl;
"1.79" << endl << endl;

"Nabisco Chips Ahoy! (item code = 9)" << endl;
"1.99" << endl << endl;

cin >> item;

cout << "Quantity: ";
cin >> quantity;

switch(item)
{
case 0: itemString = "White Rose Low Fat Yogurt, 1.99";
break;
case 1: itemString = "Diamon Crystal Salt, .89";
break;
case 2: itemString = "Trend Detergent, 1.49";
break;
case 3: itemString = "Bumble Bee Tuna, .89";
break;
case 4: itemString = "Carmela Corned Beef, 2.99";
break;
case 5: itemString = "Goya Olive Oil, 4.99";
break;
case 6: itemString = "Friendly's Ice Cream, 2.99";
break;
case 7: itemString = "Bird's Eye Mixed Vegetables, .99";
break;
case 8: itemString = "Prego Pasta Sauce, 1.79";
break;
case 9: itemString = "Nabisco Chips Ahoy, 1.99";

default: itemString = "Invalid entry";
break;


total += quantity;

}

Again, any help is welcome.
Last edited on
Please, explain
1
2
cin >> val;
if(!c.add_purchase(val)) break;;


(And use code tags.)
Disregard. The focus is adding the new string to the vector from int main.

Vector name:

vector<GroceryItem> shoppingCart;

Item function:

newItem(Grocery Item a); (header)

Function definition:

1
2
3
4
void Customer::newItem(GroceryItem a)// function to add new item to cart (implementation)
 {
 shoppingCart.push_back(a); 
 }
Last edited on
Disregard.

Negative.

That code fragment has object with name c. That object calls its member function add_purchase.

The implementation of newItem reveals that it is a member function of some type Customer. What you don't show is whether the shoppingCart is also a member variable of type Customer.

If you are supposed to use a member function of a type, then you should have an object of that type.

Furthermore, your code implies that you have type GroceryItem but you have told us nothing about it.
Right then...I managed to get more of it done. Now my question is:

1
2
3
4
5
6
7
8
9
10
11
void Customer::receipt()
{
     cout << "------------------------------------------------------" << endl << endl;
     for(int i=0;i<shoppingCart.get_size();i++)
     {
             cout << left << setw(35) << shoppingCart[i].getItemName() << right << shoppingCart[i].getItemPrice() << endl;
     }
     
     cout << left << setw(35) << "Amount Due: " << right << get_unpaid_balance() << endl;
     cout << left << setw(35) << "Credit Available: " << right << get_credit_limit() - get_unpaid_balance() << endl;
}


How do I call this implementation file function in main?
Last edited on
instantiate a Customer object and call receipt() on it.

1
2
3

Customer customer;
customer.receipt();


Assuming you've #include'd your "Customer.h" (or whatever you've called it).
Topic archived. No new replies allowed.