Reputation: 71
The code works fine in main without separate files for the 2 classes but when I try to separate them I get many errors saying ‘ItemToPurchase’ was not declared in this scope
. I assume its because I'm using ItemToPurchase
class in ShoppingCart
class but I can't get it to recognize where the implementation is located
I tried something like this (below) in the ShoppingCart.h
file but it has no effect, I'm not sure if I am missing something else did It incorrectly.
#ifndef ShoppingCart_h
#define ShoppingCart_h
#ifndef ItemToPurchase_h
#define ItemToPurchase_h
I posted the working code below
I'm not sure how to set up the file headers for the 5 files, main
and 4 .h
files
It would be ideal if someone could show how to set up the headers, I'm a student and a bit lost at this point
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class ItemToPurchase
{
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
public:
ItemToPurchase()
{
itemName = "";
itemPrice = 0;
itemQuantity = 0;
itemDescription = "none";
}
ItemToPurchase(string name, string description, int price, int quantity)
{
itemName = name;
itemDescription = description;
itemPrice = price;
itemQuantity = quantity;
}
void SetName(string name)
{
itemName = name;
}
void SetPrice(int price)
{
itemPrice = price;
}
void SetQuantity(int quantity)
{
itemQuantity = quantity;
}
string GetName()
{
return itemName;
}
int GetPrice()
{
return itemPrice;
}
int GetQuantity()
{
return itemQuantity;
}
void SetDescription(string description)
{
itemDescription = description;
}
string GetDescription()
{
return itemDescription;
}
void PrintItemCost()
{
cout << itemName << " " << itemQuantity << " @ $" << itemPrice << " = $" << itemQuantity * itemPrice << endl;
}
void PrintItemDescription()
{
cout << itemName << ": " << itemDescription << endl;
}
};
//---------------------------------------------------------------
class ShoppingCart
{
private:
string customerName;
string currentDate;
vector <ItemToPurchase> cartItems;
public:
ShoppingCart()
{
customerName = "none";
currentDate = "January 1, 2016";
}
ShoppingCart(string name, string date)
{
customerName = name;
currentDate = date;
}
string GetCustomerName()
{
return customerName;
}
string GetDate()
{
return currentDate;
}
void AddItem(ItemToPurchase newItem)
{
cartItems.push_back(newItem);
}
void RemoveItem(string name)
{
bool temp = true;
for (int i = 0; i < cartItems.size(); i++)
{
if (name == cartItems[i].GetName())
{
cartItems.erase(cartItems.begin() + i);
temp = false;
}
}
if (temp == true)
{
cout << "Item not found in cart. Nothing removed." << endl << endl;
}
else
{
cout << endl;
}
}
void ModifyItem(int quantity, string name)
{
bool temp = true;
for (int i = 0; i < cartItems.size(); i++)
{
if (name == cartItems[i].GetName())
{
cartItems[i].SetQuantity(quantity);
temp = false;
}
}
if (temp == true)
{
cout << "Item not found in cart. Nothing modified." << endl << endl;
}
else
{
cout << endl;
}
}
int GetNumItemsInCart()
{
int total = 0;
for (int i = 0; i < cartItems.size(); i++)
{
total += cartItems[i].GetQuantity();
}
return total;
}
int GetCostOfCart()
{
int total = 0;
for (int i = 0; i < cartItems.size(); i++)
{
total += cartItems[i].GetPrice() * cartItems[i].GetQuantity();
}
return total;
}
void PrintTotal()
{
if (cartItems.size() == 0)
{
cout << GetCustomerName() << "'s Shopping Cart - " << GetDate() << endl;
cout << "Number of Items: 0" << endl << endl;
cout << "SHOPPING CART IS EMPTY" << endl << endl;
cout << "Total: $0" << endl << endl;
}
else
{
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 << endl << "Total: $" << GetCostOfCart() << endl << endl;
}
}
void PrintDescriptions()
{
cout << "Item Descriptions" << endl;
for (int i = 0; i < cartItems.size(); i++)
{
cartItems[i].PrintItemDescription();
}
cout << endl;
}
};
int main()
{
cout << "Enter customer's name:" << endl;
string name;
getline(cin, name);
cout << "Enter today's date:" << endl << endl;
string date;
getline(cin, date);
cout << "Customer name: " << name << endl;
cout << "Today's date: " << date << endl << endl;
ShoppingCart customer(name, date);
bool on = true;
bool check = true;
string select;
while (on)
{
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 << endl;
cout << "Choose an option:" << endl;
cin >> select;
while (check)
{
if (select == "a" || select == "d" || select == "c" || select == "i" || select == "o" || select == "q")
{
check = false;
}
else
{
cout << "Choose an option:" << endl;
cin >> select;
}
}
check = true;
if (select == "a")
{
cout << "ADD ITEM TO CART" << endl;
cout << "Enter the item name:" << endl;
cin.ignore();
string name;
getline(cin, name);
cout << "Enter the item description:" << endl;
string description;
getline(cin, description);
cout << "Enter the item price:" << endl;
int price;
cin >> price;
cout << "Enter the item quantity:" << endl << endl;
int quantity;
cin >> quantity;
ItemToPurchase temp(name, description, price, quantity);
customer.AddItem(temp);
}
else if (select == "d")
{
cout << "REMOVE ITEM FROM CART" << endl;
cout << "Enter name of item to remove:" << endl;
cin.ignore();
string name;
getline(cin, name);
customer.RemoveItem(name);
}
else if (select == "c")
{
cout << "CHANGE ITEM QUANTITY" << endl;
cout << "Enter the item name:" << endl;
cin.ignore();
string name;
getline(cin, name);
cout << "Enter the new quantity:" << endl;
int quantity;
cin >> quantity;
customer.ModifyItem(quantity, name);
}
else if (select == "i")
{
cout << "OUTPUT ITEMS' DESCRIPTIONS" << endl;
cout << customer.GetCustomerName() << "'s Shopping Cart - " << customer.GetDate() << endl << endl;
customer.PrintDescriptions();
}
else if (select == "o")
{
cout << "OUTPUT SHOPPING CART" << endl;
customer.PrintTotal();
}
else if (select == "q")
{
on = false;
}
}
}
Upvotes: 1
Views: 172
Reputation: 1062
This is really a start at the bottom and work your way up problem. Firstly, with a header you'll usually have a header guard, some people use #pragma once some people use #ifndef/#define/#endif. I prefer the latter personally, but work at a place where #pragma once is the the standard. So you usually follow the standards of where you're working or what you've been told.
Next bit of help is the use of:
using namespace std;
I personally don't like it as you end up polluting the namespace, but again, it's about the standards and following the coding practises. It's usually only a way of avoiding having to type too many std:: scopes before a class name.
With that being said, let's get onto the code... At the bottom you have ItemToPurchase this includes no other classes except standard C++ ones, so you end up with:
// ItemToPurchase.h
#pragma once
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class ItemToPurchase
{
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
public:
ItemToPurchase()
{
itemName = "";
itemPrice = 0;
itemQuantity = 0;
itemDescription = "none";
}
ItemToPurchase(string name, string description, int price, int quantity)
{
itemName = name;
itemDescription = description;
itemPrice = price;
itemQuantity = quantity;
}
void SetName(string name) { itemName = name; }
void SetPrice(int price) { itemPrice = price; }
void SetQuantity(int quantity) { itemQuantity = quantity; }
string GetName() { return itemName; }
int GetPrice() { return itemPrice; }
int GetQuantity() { return itemQuantity; }
void SetDescription(string description) { itemDescription = description; }
string GetDescription() { return itemDescription; }
void PrintItemCost() {
cout << itemName << " " << itemQuantity << " @ $" << itemPrice << " = $" << itemQuantity * itemPrice << endl; }
void PrintItemDescription() { cout << itemName << ": " << itemDescription << endl; }
};
Just as a bit of a side-note, you should maybe const qualify the Get methods. Anyway, the above can be put into a ItemToPurchase.h header file as it contains everything that it needs.
Next you have the ShoppingCart class. This specifically names ItemToPurchase so it's going to need to include that header. It should also include the headers for the standard types and classes that it uses. This is usually the best way to ensure that a header isn't completely dependent on a header that another header may have included.
// ShoppingCart.h
#pragma once
#include "ItemToPurchase.h"
#include <vector>
#include <iostream>
#include <string>
class ShoppingCart
{
private:
string customerName;
string currentDate;
vector <ItemToPurchase> cartItems;
public:
ShoppingCart()
{
customerName = "none";
currentDate = "January 1, 2016";
}
ShoppingCart(string name, string date)
{
customerName = name;
currentDate = date;
}
string GetCustomerName() { return customerName; }
string GetDate() { return currentDate; }
void AddItem(ItemToPurchase newItem) { cartItems.push_back(newItem); }
void RemoveItem(string name)
{
bool temp = true;
for (int i = 0; i < cartItems.size(); i++)
{
if (name == cartItems[i].GetName())
{
cartItems.erase(cartItems.begin() + i);
temp = false;
}
}
if (temp == true)
{
cout << "Item not found in cart. Nothing removed." << endl << endl;
}
else
{
cout << endl;
}
}
void ModifyItem(int quantity, string name)
{
bool temp = true;
for (int i = 0; i < cartItems.size(); i++)
{
if (name == cartItems[i].GetName())
{
cartItems[i].SetQuantity(quantity);
temp = false;
}
}
if (temp == true)
{
cout << "Item not found in cart. Nothing modified." << endl << endl;
}
else
{
cout << endl;
}
}
int GetNumItemsInCart()
{
int total = 0;
for (int i = 0; i < cartItems.size(); i++)
{
total += cartItems[i].GetQuantity();
}
return total;
}
int GetCostOfCart()
{
int total = 0;
for (int i = 0; i < cartItems.size(); i++)
{
total += cartItems[i].GetPrice() * cartItems[i].GetQuantity();
}
return total;
}
void PrintTotal()
{
if (cartItems.size() == 0)
{
cout << GetCustomerName() << "'s Shopping Cart - " << GetDate() << endl;
cout << "Number of Items: 0" << endl << endl;
cout << "SHOPPING CART IS EMPTY" << endl << endl;
cout << "Total: $0" << endl << endl;
}
else
{
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 << endl << "Total: $" << GetCostOfCart() << endl << endl;
}
}
void PrintDescriptions()
{
cout << "Item Descriptions" << endl;
for (int i = 0; i < cartItems.size(); i++)
{
cartItems[i].PrintItemDescription();
}
cout << endl;
}
};
Now we're done with the ShoppingCart.h and the ItemToPurchase.h header files and both are protected with #pragma once.
Finally, we're onto our main function which will be contained in a .cpp file. Now this function names both ItemToPurchase and ShoppingCart by name, so it should include both header files. You could get away with just including ShoppingCart.h as it includes ItemToPurcahse.h, but it's good practice to include all of headers of the types that you name.
Finally this gives you a main.cpp file which should be:
// main.cpp
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
#include <iostream>
#include <string>
int main()
{
cout << "Enter customer's name:" << endl;
string name;
getline(cin, name);
cout << "Enter today's date:" << endl << endl;
string date;
getline(cin, date);
cout << "Customer name: " << name << endl;
cout << "Today's date: " << date << endl << endl;
ShoppingCart customer(name, date);
bool on = true;
bool check = true;
string select;
while (on)
{
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 << endl;
cout << "Choose an option:" << endl;
cin >> select;
while (check)
{
if (select == "a" || select == "d" || select == "c" || select == "i" || select == "o" || select == "q")
{
check = false;
}
else
{
cout << "Choose an option:" << endl;
cin >> select;
}
}
check = true;
if (select == "a")
{
cout << "ADD ITEM TO CART" << endl;
cout << "Enter the item name:" << endl;
cin.ignore();
string name;
getline(cin, name);
cout << "Enter the item description:" << endl;
string description;
getline(cin, description);
cout << "Enter the item price:" << endl;
int price;
cin >> price;
cout << "Enter the item quantity:" << endl << endl;
int quantity;
cin >> quantity;
ItemToPurchase temp(name, description, price, quantity);
customer.AddItem(temp);
}
else if (select == "d")
{
cout << "REMOVE ITEM FROM CART" << endl;
cout << "Enter name of item to remove:" << endl;
cin.ignore();
string name;
getline(cin, name);
customer.RemoveItem(name);
}
else if (select == "c")
{
cout << "CHANGE ITEM QUANTITY" << endl;
cout << "Enter the item name:" << endl;
cin.ignore();
string name;
getline(cin, name);
cout << "Enter the new quantity:" << endl;
int quantity;
cin >> quantity;
customer.ModifyItem(quantity, name);
}
else if (select == "i")
{
cout << "OUTPUT ITEMS' DESCRIPTIONS" << endl;
cout << customer.GetCustomerName() << "'s Shopping Cart - " << customer.GetDate() << endl << endl;
customer.PrintDescriptions();
}
else if (select == "o")
{
cout << "OUTPUT SHOPPING CART" << endl;
customer.PrintTotal();
}
else if (select == "q")
{
on = false;
}
}
}
And that should all work, with your class definitions separated out into their respective .h files.
Upvotes: 1