Question: I need help debugging my Online shopping cart program as I'm getting these strange errors. I've tried to fix them but they just won't go

I need help debugging my Online shopping cart program as I'm getting these strange errors. I've tried to fix them but they just won't go away.

ShoppingCart.cpp: In member function 'int ShoppingCart::GetNumItemsInCart()': ShoppingCart.cpp:110:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector::size_type' {aka 'long unsigned int'} [-Wsign-compare] 110 | for (int i = 0; i < cartItems.size(); i++) { | ~~^~~~~~~~~~~~~~~~~~ ShoppingCart.cpp: In member function 'double ShoppingCart::GetCostOfCart()': ShoppingCart.cpp:128:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector::size_type' {aka 'long unsigned int'} [-Wsign-compare] 128 | for (int i = 0; i < cartItems.size(); i++) { | ~~^~~~~~~~~~~~~~~~~~ ShoppingCart.cpp: In member function 'void ShoppingCart::PrintTotal()': ShoppingCart.cpp:154:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector::size_type' {aka 'long unsigned int'} [-Wsign-compare] 154 | for (int i = 0; i < cartItems.size(); i++) { | ~~^~~~~~~~~~~~~~~~~~ ShoppingCart.cpp: In member function 'void ShoppingCart::PrintDescriptions()': ShoppingCart.cpp:179:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector::size_type' {aka 'long unsigned int'} [-Wsign-compare] 179 | for (int i = 0; i < cartItems.size(); i++) { | ~~^~~~~~~~~~~~~~~~~~ /usr/bin/ld: /tmp/ccxEspyU.o: in function `ExecuteMenu(std::__cxx11::basic_string, std::allocator >, ShoppingCart&)': main.cpp:(.text+0x30b): undefined reference to `ItemToPurchase::SetName(std::__cxx11::basic_string, std::allocator >)' /usr/bin/ld: main.cpp:(.text+0x343): undefined reference to `ItemToPurchase::SetDescription(std::__cxx11::basic_string, std::allocator >)' /usr/bin/ld: main.cpp:(.text+0x58b): undefined reference to `ItemToPurchase::SetName(std::__cxx11::basic_string, std::allocator >)' /usr/bin/ld: /tmp/ccWoK0KT.o: in function `ShoppingCart::ModifyItem(ItemToPurchase)': ShoppingCart.cpp:(.text+0x48b): undefined reference to `ItemToPurchase::SetDescription(std::__cxx11::basic_string, std::allocator >)' /usr/bin/ld: ShoppingCart.cpp:(.text+0x4ef): undefined reference to `ItemToPurchase::GetQuantity()' /usr/bin/ld: ShoppingCart.cpp:(.text+0x507): undefined reference to `ItemToPurchase::GetQuantity()' /usr/bin/ld: /tmp/ccWoK0KT.o: in function `ShoppingCart::GetNumItemsInCart()': ShoppingCart.cpp:(.text+0x64f): undefined reference to `ItemToPurchase::GetQuantity()' /usr/bin/ld: /tmp/ccWoK0KT.o: in function `ShoppingCart::GetCostOfCart()': ShoppingCart.cpp:(.text+0x6c1): undefined reference to `ItemToPurchase::GetQuantity()' collect2: error: ld returned 1 exit status

Here is my code files:

main.cpp

#include  #include "ShoppingCart.h" using namespace std; void PrintMenu() { cout << endl << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; cout << "Choose an option:" << endl; } void ExecuteMenu(string option, ShoppingCart &shopcart) { string itname, itdesc; double itprice; int itquant; if (option == "a") { cout << "ADD ITEM TO CART" << endl; cout << "Enter the item name:" << endl; getline(cin, itname); cout << "Enter the item description:" << endl; getline(cin, itdesc); cout << "Enter the item price:" << endl; cin >> itprice; cout << "Enter the item quantity:" << endl; cin >> itquant; ItemToPurchase addAnItem; addAnItem.SetName(itname); addAnItem.SetDescription(itdesc); addAnItem.SetPrice(itprice); addAnItem.SetQuantity(itquant); shopcart.AddItem(addAnItem); } else if (option == "d") { cout << "REMOVE ITEM FROM CART" << endl; cout << "Enter name of item to remove:" << endl; getline(cin, itname); shopcart.RemoveItem(itname); } else if (option == "c") { cout << "CHANGE ITEM QUANTITY" << endl; ItemToPurchase chitem; cout << "Enter the item name:" << endl; getline(cin, itname); cout << "Enter the new quantity:" << endl; cin >> itquant; chitem.SetName(itname); chitem.SetQuantity(itquant); shopcart.ModifyItem(chitem); } else if (option == "i") { cout << "OUTPUT ITEMS' DESCRIPTIONS" << endl; shopcart.PrintDescriptions(); } else if (option == "o") { cout << "OUTPUT SHOPPING CART" << endl; shopcart.PrintTotal(); } else if (option == "q"){ //quit } else { //invalid choice cout << endl; } } int main() { string cName; string currDate; string option; cout << "Enter customer's name:" << endl; getline(cin, cName); cout << "Enter today's date:"<< endl << endl; getline(cin, currDate); ShoppingCart shcart(cName, currDate); cout << "Customer name: " << shcart.GetCustomerName() << endl; cout << "Today's date: " << shcart.GetDate() << endl; while (true) { PrintMenu(); cin >> option; cin.ignore(); ExecuteMenu(option, shcart); if (option == "q") { break; } } return 0; }

ShoppingCart.cpp

#include "ShoppingCart.h" ShoppingCart::ShoppingCart() { customerName = "none"; currentDate = "January 1, 2016"; } ShoppingCart::ShoppingCart(string cname, string curdate) { customerName = cname; currentDate = curdate; } string ShoppingCart::GetCustomerName() { return customerName; } string ShoppingCart::GetDate() { return currentDate; } void ShoppingCart::AddItem(ItemToPurchase it) { cartItems.push_back(it); } void ShoppingCart::RemoveItem(string itname) { for (vector::iterator it = cartItems.begin(); it != cartItems.end(); it++) { if ((*it).GetName() == itname) { cartItems.erase(it); return; } } cout << "Item not found in cart. Nothing removed." << endl; } void ShoppingCart::ModifyItem(ItemToPurchase it) { bool found = false; for (vector::iterator itemList = cartItems.begin(); itemList != cartItems.end(); itemList++) { if ((*itemList).GetName() == it.GetName()) { found = true; ItemToPurchase& item = *itemList; if (it.GetDescription() != "none") { item.SetDescription(it.GetDescription()); } if (it.GetPrice() != 0) { item.SetPrice(it.GetPrice()); } if (it.GetQuantity() != 0) { item.SetQuantity(it.GetQuantity()); } } } if (found != true) { cout << "Item not found in cart. Nothing modified." << endl; } } int ShoppingCart::GetNumItemsInCart() { int numOfQuantityInCart = 0; for (int i = 0; i < cartItems.size(); i++) { numOfQuantityInCart = numOfQuantityInCart + cartItems[i].GetQuantity(); } return numOfQuantityInCart; } double ShoppingCart::GetCostOfCart() { int totalCost = 0; for (int i = 0; i < cartItems.size(); i++) { totalCost += cartItems[i].GetQuantity() *cartItems[i].GetPrice(); } return totalCost; } void ShoppingCart::PrintTotal() { if (cartItems.empty()) { cout << "SHOPPING CART IS EMPTY" << endl; } cout << customerName << "'s Shopping Cart - " << currentDate << endl; cout << "Number of Items: " << GetNumItemsInCart() << endl << endl; for (int i = 0; i < cartItems.size(); i++) { cartItems[i].PrintItemCost(); } cout << " Total: $" << GetCostOfCart() << endl; } void ShoppingCart::PrintDescriptions() { if (cartItems.empty()) { cout << "SHOPPING CART IS EMPTY" << endl; } cout << customerName << "'s Shopping Cart - " << currentDate << endl << endl; cout << "Item Descriptions" << endl; for (int i = 0; i < cartItems.size(); i++) { cartItems[i].PrintItemDescription(); } }

ShoppingCart.h

#pragma once #ifndef ShoppingCart_h #define ShoppingCart_h #include  #include  #include "ItemToPurchase.h" using namespace std; class ShoppingCart { private: string customerName; string currentDate; vector cartItems; public: ShoppingCart(); ShoppingCart(string cname, string curdate); string GetCustomerName(); string GetDate(); void AddItem(ItemToPurchase it); void RemoveItem(string itname); void ModifyItem(ItemToPurchase it); int GetNumItemsInCart(); double GetCostOfCart(); void PrintTotal(); void PrintDescriptions(); }; #endif

ItemToPurchase.cpp

#include "ItemToPurchase.h" ItemToPurchase::ItemToPurchase() { itemName = "none"; itemDescription = "none"; itemPrice = 0; itemQuantity = 0; } ItemToPurchase::ItemToPurchase(string iname, string idesc, int iquant, int iprice) { itemName = iname; itemDescription = idesc; itemQuantity = iquant; itemPrice = iprice; } string ItemToPurchase::GetName() { return itemName; } void ItemToPurchase::SetPrice(double itprice) { itemPrice = itprice; } double ItemToPurchase::GetPrice() { return itemPrice; } void ItemToPurchase::SetQuantity(int itquantity) { itemQuantity = itquantity; } string ItemToPurchase::GetDescription() { return itemDescription; } void ItemToPurchase::PrintItemCost() { cout << itemName << " " << itemQuantity << " @ $" << itemPrice << " = $" << (itemQuantity * itemPrice) << endl; } void ItemToPurchase::PrintItemDescription() { cout << itemName << ": " << itemDescription << endl; }

ItemToPurchase.h

#pragma once #ifndef ItemToPurchase_h #define ItemToPurchase_h #include  #include  #include  using namespace std; class ItemToPurchase { private: string itemName; string itemDescription; int itemQuantity; double itemPrice; public: ItemToPurchase(); ItemToPurchase(string iname, string idesc, int iquant, int iprice); void SetName(string itname); string GetName(); void SetPrice(double itprice); double GetPrice(); void SetQuantity(int itquantity); int GetQuantity(); void SetDescription(string itdesc); string GetDescription(); void PrintItemCost(); void PrintItemDescription(); }; #endif

Thanks!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!