Help with updating the cart function from a class

Hello I am having a really hard time trying to get my Update Quantity function to work. And I'm not sure why. Everything else in my code works.
Here is the specs: Update the quantity of an item object in the cart
If item name cannot be found, output this message: "Item not found in cart. Nothing modified." If the quantity is set to 0, still leave the item in the cart, but treat it as a 0 in all counts, etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//This is the function I call in the change cart option. This is the function //what I need help with. 
void ShoppingCart::UpdateQuantity(string itemName, int itemQuantity) {
	string itemName;
	int found = 0;
	int newQuantity = 0;
	itemQuantity = newQuantity;
	cout << "Enter the item name: " << endl;
	cin.ignore();
	getline(cin, itemName);
	cout << "Enter the new quantity: " << endl;*/
	cin >> newQuantity;
	if (found != NOT_FOUND) {
		UpdateQuantity(newQuantity);
	}
	else {
		cout << "Item not found in cart. Nothing modified" << endl;
	}
}  


//Here is the int main code.
int main() {
	srand(time(0));
	//vector<string> restaurants;
	int menu = 1;
	bool done = false;
	int menuOption = 1;
	int cutChoice = 1;

	string userInput = "";
	string custName = "";
	string custDate = "";
	string removeItemNew = "";
	const string ADD_OPTION = "add";
	const string REMOVE_ITEM = "remove";
	const string CHANGE_ITEM = "change";
	const string DESCRIPTIONS_ITEM = "descriptions";
	const string SHOP_CART = "cart";
	const string OPTIONS_MENU = "options";
	const string QUIT_MENU = "quit";
	string itemName = " ";
	string itemDescription = " ";
	int newQuantity = 0;
	double itemPrice = 0.0;
	int itemQuantity = 0;

	//object
	ShoppingCart Cart;
	//ItemToPurchase Item;

	cout << "Enter Customer's Name: " << endl;
	//cin.ignore();
	getline(cin, custName);
	cout << "Enter Today's Date: " << endl;
	//cin.ignore();
	getline(cin, custDate);



	while (!done) {
		cout << "Enter option: " << endl;
		cin >> userInput;
		if (userInput == OPTIONS_MENU) {
			printMenu();
			cout << endl;
		}
		else if (userInput == ADD_OPTION) {

			//Cart.AddItem(Item);

			cout << "Enter the item name:" << endl;
			cin.ignore();
			getline(cin, itemName);
			cout << "Enter the item description: " << endl;
			//cin.ignore();
			getline(cin, itemDescription);
			cout << "Enter the item price: " << endl;
			cin >> itemPrice;
			cout << "Enter the item quantity: " << endl;
			cin >> itemQuantity;
			Cart.AddItem(itemName, itemDescription, itemPrice, itemQuantity);
		}
		//work in progress change item this is what I am trying to get to work.
		else if (userInput == CHANGE_ITEM) {
			/*
			cout << "Enter the item name:" << endl;
			getline(cin, itemName);
			cout << "Enter the new quantity:" << endl;
			cin >> newQuantity;*/
			Cart.UpdateQuantity(itemName, newQuantity);

		}

		else if (userInput == QUIT_MENU) {
			menu = 0;
			cout << endl;
			cout << "Goodbye." << endl;
			return 0;
		}
		else {
			cin.clear();
			cin.ignore(BLASTED_ONE_THOUSAND, '\n');
			//cout << "Invalid option " << endl;
			printMenu();
		}
	}
	return(0);
It's clear what you're trying to do with the function, but the interaction between the UpdateQuantity function and the class needs to be reworked. It may be helpful to post your class declaration for ShoppingCart so that we can see other functions the class provides.

1) In order to update an item's quantity, you need a function that finds the item. This should be it's own function rather than part of UpdateQuantity(). Once find works, UpdateQuantity() can call find and update the data structure used to store item counts.

2) I recommend removing the lines to gather user input outside of the function (and probably the class) altogether. Your main() or another function should get user input, and then pass that input to ShoppingCart::UpdateQuantity(string itemName, int itemQuantity) as parameters.

3) Most likely, UpdateQuantity() should not call itself.

Following the above recommendations basically guts the code you have, but will put you in a better position to get the function working.

Here's a super brief example of how it could look. FYI, I don't know how you store your items and quantities in ShoppingCart, so I'm going to pretend that you store item names and quantities in two matching vectors of equal length and positions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

class ShoppingCart {
public:
    void UpdateQuantity(string itemName, int itemQuantity);
    ...
private:
    int FindItem(string itemName);
    vector<string> allItemNames;
    vector<int> allItemCounts;
    const int ITEM_NOT_FOUND = -1;
    ...
};


// returns index location of itemName or ITEM_NOT_FOUND
int ShoppingChart::FindItem(string itemName)
{
    int index = ITEM_NOT_FOUND;  // assume item is not found to begin
    for (int i = 0; i < allItemNames.size(); i++)
    {
        if (itemName == allItemNames[i])   // item was found
            index = i;                       // remember the vector index
    }
    return index;
}

// changes the quantity of an item if found in cart
void ShoppingCart::UpdateQuantity(string itemName, int itemQuantity)
{
    int index = FindItem(itemName);  // get index of item
    
    if (index == ITEM_NOT_FOUND)
        cout << "Item not found in cart." << endl;
    else {
        allItemCounts[index] = itemQuantity;  // update count
        cout << "Updated quanity of " << itemName << " to " << itemQuantity << endl;
    }
}


That's only a simple skeleton and may not be compatible with they way your ShoppingCart class stores it's data members, but hopefully it's easy to understand and gets you thinking in the right direction.

Remember that the goal of most functions is to be simple and to do one thing. It's easy to write giant functions when you're unsure which puzzle pieces you need and how they connect together, but it's better to have more pieces that are small and manageable than fewer big and messy ones.

Good luck!
Last edited on
Here is my class declaration for my Shopping Cart
#pragma once
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <math.h>
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <limits>
#include "ItemToPurchase.h"
using namespace std;
class ShoppingCart {
private:
string custName;
string custDate;
vector<ItemToPurchase*> itemsToPurchase;
int itemQuantity;
public:
ShoppingCart();
void CustName(string name);
void CartDate(string description);
void AddItem(string itemName, string itemDescription, double itemPrice, int itemQuantity);
void RemoveItem(string itemName);
void UpdateQuantity(string itemName, int newQuantity);
void PrintDescriptions(string itemName);
void PrintTotal();
int FindItem(string itemName);
double GetCostCart();
string GetCustName();
string GetCartDate();
string AddItem();
string GetRemoveItem();
int GetQuantity();
};
Why all of those #include statements? It appears that all you should need in this file is <string> and <vector> and you would either need to #include the <ItemToPurchase.h" file or forward declare the ItemToPurchase class.

Also you shouldn't have that using namespace std; statement in a header file, properly scope the items instead (std::string). Also you really should be passing the std::strings by reference/const reference to avoid the unnecessary copy operations.
I keep on getting an error stating a value type "int" cannot be assigned to an entity of type
allItemCounts[index] for the line 35 in the code. For this line of code
allItemCounts[index] = itemQuantity;

What does that exactly mean? I am sorry, I am just trying to understand what I am doing wrong.
It's hard to say why you're getting an error since we only see fragments of your code, but here's a guess.

The example code for ShoppingCart::UpdateQuantity function had line:

allItemCounts[index] = itemQuantity; // update count

In my earlier example, items and item counts were stored separately in vectors. If you store items in a different way (and I think you do), that line will need to be modified or removed.

Your ShoppingCart class declaration indicates that items are stored in a vector of ItemToPurchase pointers. This means the example code cannot simply be plugged in to your program, it needs to be changed to fit your design.

Here's a related question that you probably need to answer:

How does your ShoppingCart store the quantity for each ItemToPurchase in the cart?
Topic archived. No new replies allowed.