I finished most of the program but having trouble with some

I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I included my code at the bottom. Thanks!

1. I can\'t get my RemoveItem() and ModifyItem() functions to work.

2. GetNumItemsInCart() is returning the wrong value. For example, my cart has 5 chocolate chip cookies, 1 headphone, and 2 sneakers. GetNumItemsInCart() returns 3 instead of 8 when I output my shopping cart.

3. When PrintDescriptions() is called, the first letter of my item description is missing. For example, for the chocolate chip cookies, the description is Semi-sweet. When PrintDescriptions() is called, it returns emi-sweet, with the S gone.

4. Shopping Cart improperly initialized with default constructor , and I don\'t know why.

12.9 Program: Online shopping cart (continued) (C++)

This program extends the earlier \"Online shopping cart\" program.

(1) Extend the ItemToPurchase class per the following specifications:

Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)

Public member functions

SetDescription() mutator & GetDescription() accessor (2 pts)

PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal

PrintItemDescription() - Outputs the item name and description

Private data members

string itemDescription - Initialized in default constructor to \"none\"

Ex. of PrintItemCost() output:


Ex. of PrintItemDescription() output:


(2) Create three new files:

ShoppingCart.h - Class declaration

ShoppingCart.cpp - Class definition

main.cpp - main() function (Note: main()\'s functionality differs from the warm up)

Build the ShoppingCart class with the following specifications. Note: Some can be function stubs (empty functions) initially, to be completed in later steps.

Default constructor

Parameterized constructor which takes the customer name and date as parameters (1 pt)

Private data members

string customerName - Initialized in default constructor to \"none\"

string currentDate - Initialized in default constructor to \"January 1, 2016\"

vector < ItemToPurchase > cartItems

Public member functions

GetCustomerName() accessor (1 pt)

GetDate() accessor (1 pt)

AddItem()

Adds an item to cartItems vector. Has parameter ItemToPurchase. Does not return anything.

RemoveItem()

Removes item from cartItems vector. Has a string (an item\'s name) parameter. Does not return anything.

If item name cannot be found, output this message: Item not found in cart. Nothing removed.

ModifyItem()

Modifies an item\'s description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything.

If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify item in cart.

If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified.

GetNumItemsInCart() (2 pts)

Returns quantity of all items in cart. Has no parameters.

GetCostOfCart() (2 pts)

Determines and returns the total cost of items in cart. Has no parameters.

PrintTotal()

Outputs total of objects in cart.

If cart is empty, output this message: SHOPPING CART IS EMPTY

PrintDescriptions()

Outputs each item\'s description.


Ex. of PrintTotal() output:


Ex. of PrintDescriptions() output:


(3) In main(), prompt the user for a customer\'s name and today\'s date. Output the name and date. Create an object of type ShoppingCart. (1 pt)

Ex.


(4) Implement the PrintMenu() function. PrintMenu() has a ShoppingCart parameter, and outputs a menu of options to manipulate the shopping cart. Each option is represented by a single character. Build and output the menu within the function.

If the an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to execute the menu until the user enters q to Quit. (3 pts)

Ex:


(5) Implement Output shopping cart menu option. (3 pts)

Ex:


(6) Implement Output item\'s description menu option. (2 pts)

Ex.


(7) Implement Add item to cart menu option. (3 pts)

Ex:


(8) Implement remove item menu option. (4 pts)

Ex:


(9) Implement Change item quantity menu option. Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using ModifyItem() function. (5 pts)

Ex:

My Program:

ItemToPurchase.h

#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H

#include <iostream>
#include <string>
using namespace std;

class ItemToPurchase {
   public:
       ItemToPurchase();
       ItemToPurchase(string itemName, double itemPrice, int itemQuantity, string itemDescription);
      
       void SetName(string itemName);
       void SetPrice(double itemPrice);
       void SetQuantity(int itemQuantity);
       void SetDescription(string itemDescription);
      
       string GetName();
       double GetPrice();
       int GetQuantity();
       string GetDescription();

       void PrintItemCost();
       void PrintItemDescription();

   private:
       string itemName;
       double itemPrice;
       int itemQuantity;
       string itemDescription;
};

#endif

ItemToPurchase.cpp

#include <iostream>
#include <string>
#include \"ItemToPurchase.h\"
using namespace std;

ItemToPurchase::ItemToPurchase() {
   itemName = \"NoName\";
itemPrice = 0.0;
itemQuantity = 0;
   itemDescription = \"NoDescription\";
   return;
}

ItemToPurchase::ItemToPurchase(string itemName, double itemPrice, int itemQuantity, string itemDescription) {
   SetName(itemName);
   SetPrice(itemPrice);
   SetQuantity(itemQuantity);
   SetDescription(itemDescription);
   return;
}

void ItemToPurchase::SetName(string item_Name) {
itemName = item_Name;
   return;
}

void ItemToPurchase::SetPrice(double item_Price) {
itemPrice = item_Price;
   return;
}

void ItemToPurchase::SetQuantity(int item_Quantity) {
itemQuantity = item_Quantity;
   return;
}

void ItemToPurchase::SetDescription(string item_Description) {
itemDescription = item_Description;
   return;
}

string ItemToPurchase::GetName() {
return itemName;
}

double ItemToPurchase::GetPrice() {
return itemPrice;
}

int ItemToPurchase::GetQuantity() {
return itemQuantity;
}

string ItemToPurchase::GetDescription() {
return itemDescription;
}

void ItemToPurchase::PrintItemCost() {
cout << itemName << \" \" << itemQuantity << \" @ $\" << itemPrice << \" = $\" << itemQuantity * itemPrice << endl;
return;
}


void ItemToPurchase::PrintItemDescription() {
cout << itemName << \": \" << itemDescription << endl;
return;
}

ShoppingCart.h

#include <iostream>
#include <string>
#include <vector>
#include \"ItemToPurchase.h\"
using namespace std;

class ShoppingCart {
   public:
   ShoppingCart();
   ShoppingCart(string customerName, string currentDate );
     
   string GetCustomerName();
   string GetDate();
     
   void AddItem(ItemToPurchase);
   void RemoveItem(string);
   void ModifyItem(ItemToPurchase);
     
   int GetNumItemsInCart();
   int GetCostOfCart();
     
   void PrintTotal();
   void PrintDescriptions();

   private:
   string customerName;
   string currentDate;
   vector <ItemToPurchase> cartItems;
};

#endif

ShoppingCart.cpp

ShoppingCart::ShoppingCart() {
   customerName = \"NoName\";
   currentDate = \"October 23, 2016\";
   return;
}

ShoppingCart::ShoppingCart(string cName, string cDate) {
   customerName = cName;
   currentDate = cDate;
   return;
}

string ShoppingCart::GetCustomerName(){
   return customerName;
}

string ShoppingCart::GetDate(){
   return currentDate ;
}

void ShoppingCart::AddItem(ItemToPurchase itemToAdd){
   cartItems.push_back(itemToAdd);
   return;
}

void ShoppingCart::RemoveItem(string itemToRemove){
   unsigned int oldsize = cartItems.size();
  
   for (unsigned int i = 0; i < cartItems.size(); ++i){
       if (cartItems.at(i).GetName() == itemToRemove) {
           cartItems.erase(cartItems.begin()+i);
       }
   }
  
   if(oldsize == cartItems.size()) {
       cout << \"Item not found in cart. Nothing removed.\" << endl;
   }
   return;
}

void ShoppingCart::ModifyItem(ItemToPurchase itemToChange){
   bool itemToModify = false;
  
   for (unsigned int i = 0; i < cartItems.size(); ++i){
       if (cartItems.at(i).GetName() == itemToChange.GetName()) {
           cartItems.at(i).SetQuantity(itemToChange.GetQuantity());
           itemToModify = true;
       }
   }
  
   if (itemToModify == false) {
       cout << \"Item not found in cart. Nothing modified.\";
   }
   return;
}

int ShoppingCart::GetNumItemsInCart(){
   int numItems = cartItems.size();
   return numItems;
}

int ShoppingCart::GetCostOfCart() {
   int sum = 0;
  
   for (unsigned int i = 0; i < cartItems.size(); ++i){
       sum = sum + (cartItems.at(i).GetPrice()*cartItems.at(i).GetQuantity());
   }
   return sum;
}

void ShoppingCart::PrintTotal(){
int total = 0;
cout << customerName << \"\'s Shopping Cart - \" << currentDate << endl;

if(cartItems.size() == 0){
       cout << \"Number of Items: \" << GetNumItemsInCart() << endl;
       cout << endl;
       cout << \"SHOPPING CART IS EMPTY\" << endl;
       cout << endl;
       cout << \"Total: $\" << total << endl;
   }
   else{
       cout << \"Number of Items: \" << GetNumItemsInCart() << endl;
       cout << endl;
      
       for(unsigned int i = 0; i < cartItems.size(); ++i) {
           cout << cartItems.at(i).GetName() << \" \" << cartItems.at(i).GetQuantity() << \" @ $\" << cartItems.at(i).GetPrice()
               << \" = $\" << cartItems.at(i).GetPrice()*cartItems.at(i).GetQuantity() << endl;
           total = total + (cartItems.at(i).GetPrice()*cartItems.at(i).GetQuantity());
       }
       cout << endl;
       cout << \"Total: $\" << total << endl;
       cout << endl;
   }
   return;
}

void ShoppingCart::PrintDescriptions(){
   cout << customerName << \"\'s Shopping Cart - \" << currentDate << endl;
   cout << endl;
  
   cout << \"Item Descriptions\" << endl;
   for(unsigned int i = 0; i < cartItems.size(); ++i){
       cout << cartItems.at(i).GetName() << \": \" << cartItems.at(i).GetDescription() << endl;
   }
   return;
}

main.cpp

int main() {
   string customerName;
   string currentDate;
  
   string itemName;
   double itemPrice;
   int itemQuantity;
   string itemDescription;
  
   string itemToRemove;
   int newQuantity;
   char userOption;
  
   cout << \"Enter Customer\'s Name: \";
   getline(cin, customerName);
   cout << \"Enter Today\'s Date: \";
   getline(cin, currentDate);
   cout << \"Customer Name: \" << customerName << endl;
   cout << \"Today\'s Date: \" << currentDate << endl;

   ShoppingCart itemCart(customerName, currentDate);
  
   while (userOption != \'q\')
   {
       cout << endl;
       cout <<\"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;
       cout << endl;
      
       cout << \"Choose an option: \";
       cin >> userOption;
       cout << endl;
      
       if (userOption == \'a\' || userOption == \'A\') {
           cout << \"ADD ITEM TO CART\" << endl;
          
           cout << \"Enter the item name: \";
           cin.ignore();
           getline (cin, itemName);
          
           cout << \"Enter the item description: \";
           cin.ignore();
           getline(cin, itemDescription);
          
           cout << \"Enter the item price: \";
           cin >> itemPrice;
          
           cout << \"Enter the item quantity: \";
           cin >> itemQuantity;

           ItemToPurchase itemToAdd(itemName, itemDescription, itemPrice, itemQuantity);
           itemCart.AddItem(itemToAdd);
       }
      
       if (userOption == \'d\' || userOption == \'D\'){
           cout << \"REMOVE ITEM FROM CART\" << endl;
           cout << \"Enter name of item to remove: \";
           getline(cin, itemToRemove);
           itemCart.RemoveItem(itemToRemove);
       }
      
       if (userOption == \'c\' || userOption == \'C\') {
           cout << \"CHANGE ITEM QUANTITY\" << endl;
           cout << \"Enter the item name: \";
           cin >> itemName;
           cout << \"Enter the new quantity: \";
           cin >> newQuantity;
          
           //ItemToPurchase itemToChange(newQuantity);
           //itemCart.ModifyItem(itemToChange);
       }
      
       if (userOption == \'i\' || userOption == \'I\') {
           cout << \"OUTPUT ITEMS\' DESCRIPTIONS\" << endl;
           itemCart.PrintDescriptions();
       }
      
       if (userOption == \'o\' || userOption == \'O\') {
           cout << \"OUTPUT SHOPPING CART\" << endl;
           itemCart.PrintTotal();
       }
   }
   return 0;
}

Solution

Your code seems code good here.No changes As I reviewed your code.

void ShoppingCart::RemoveItem(string itemToRemove){
   unsigned int oldsize = cartItems.size();
  
   for (unsigned int i = 0; i < cartItems.size(); ++i){
       if (cartItems.at(i).GetName() == itemToRemove) {
           cartItems.erase(cartItems.begin()+i);
       }
   }
  
   if(oldsize == cartItems.size()) {
       cout << \"Item not found in cart. Nothing removed.\" << endl;
   }
   return;
}

I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc
I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc
I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc
I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc
I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc
I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc
I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc
I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc
I finished most of the program, but having trouble with some key features. I bolded and italicized the parts I can\'t figure out, and included them below. I inc

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site