Need help with shopping cart code

I have a project in my comp class and I need help writing the array part for the items price and quantity for multiple items.
(maintainCart() – This function will allow the user to add multiple items to their cart. It should work similar to Project Two. It will need to maintain the complete list of products added to the cart and the subtotal. Instead of storing all of the items into a string, you will need to save each item purchased in an array. You should also save the quantity and price for each item in other arrays of the appropriate data type.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 void maintainCart (int price[], int quantity[])
{
	int subtotal;
	cout << "What is the price of the item? " << endl;
	cin >> price[];
        cout << "What is the quantity? << endl;
        cin >> quantity[];

        for(int i=0; i< 10; i++)
	{
		cin >> price[i];
		 
	}

	for (int j=0; j<10 ; j++)
	{
		cin >> quantity [j];
		

} 
closed account (o3hC5Di1)
Hi there,

You should move your questions within your for loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void maintainCart (int price[], int quantity[])
{
	int subtotal;

        for(int i=0; i< 10; i++)
	{
	    cout << "What is the price of the item? " << endl;
	    cin >> price[i];
            cout << "What is the quantity?" << endl; //also, fix the closing quote " here.
            cin >> quantity[i];

            subtotal+= price[i]*quantity[i];  //increase subtotal
	}
}


Hope that makes sense, please let us know if you have any further questions.
On a sidenote, this assumes that a shoppingcart will only maximally contain 10 items. For a dynamically sized container which can hold as many items as necessary without risking memory-errors, please see: http://www.cplusplus.com/reference/vector/

All the best,
NwN

Last edited on
Topic archived. No new replies allowed.