I've initialized two maps:
map<int, item> items
map<item, int> cart
See the following:
item milk(allSKU[0], "Horizon Milk", 50, 4.99);
item phone(allSKU[1], "iPhone 13", 50, 799.99);
item apples(allSKU[2], "Fuji Apples (6ct)", 50, 3.99);
item webcam(allSKU[3], "Webcam 1080p", 50, 49.99);
item ps5(allSKU[4], "PlayStation 5 Digital", 50, 399.99);
map<int, item> items = { { allSKU[0], milk },
{ allSKU[1], phone },
{ allSKU[2], apples },
{ allSKU[3], webcam },
{ allSKU[4], ps5 } };
display(Store);
map<item, int> cart;
int user_input;
bool still_buying = true;
while (still_buying) {
cout << " Please scan the SKU of the item (Press 0 to finish): ";
cin >> user_input;
if (user_input == 0) {
break;
} // Error here?
else if (std::find(allSKU.begin(), allSKU.end(), user_input) != allSKU.end()) {
// if item is already in the cart, dont add it.
// find the item's qty and +1
map<item, int>::iterator it = cart.find(items.at(user_input));
if (cart.end() != it) {
cart[items.at(user_input)]++;
}
else {
// if input is new, add item name and set qty to 1
cart.insert(pair<item, int>(items.at(user_input), 1));
}
cout << " " << items.at(user_input).getName() << " scanned!\n";
}
else {
cout << "\n This item doesnt exist!";
} // throw exception if input not an integer
}
However, I run into an error
binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator
Essentially, I created a map called cart, with two types: an item object as a key and an int; its quantity as its value. Every time a user scans a SKU, it should first check if the item is in the cart, and if it is, increment its quantity. If it's unique, then insert it and set its quantity to 1.
I believe my issue is under my else if statement, but I cannot figure out why.
Any help is appreciated.