I have a mutator function that check my private vector to see if there is already an object like it, in it. I want it to return a boolean of 'true' or 'false'. 'True' if there is already something like it in the vector, 'false' if there is not.
How could I do that with my code provided below?
I thought that by putting bool in front of "ItemToPurChase" would work but I get the error:
"Expected ';' after top level declarator".
At the moment I have my mutator return the item, because that works. But when I also want to print what is returned, it also gave me the error:
"Invalid operand to binary expression ('std::__1ostream'(aka ''basic_ostream<char>') and 'void')"
Here is the .h and .cpp files and the part of my code that I am having trouble with.
ShoppingCart.h file
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
|
#include <cstring>
#include <iostream>
#include <vector>
#include "ItemToPurchase.h"
using namespace std;
#ifndef INC_8_23_MAIN_LAB_8_SHOPPINGCART_H
#define INC_8_23_MAIN_LAB_8_SHOPPINGCART_H
class ShoppingCart {
public:
// Setters
void SetCustomerName(string name);
void SetCartDate(string date);
// Getters
string GetCustomerName() const;
string GetCartDate() const;
// Functions
void AddItem(ItemToPurchase item);
void RemoveItem();
void UpdateQuantity();
void TotalQuantity();
void TotalCost();
// Prints
void TotalQuantityAndCost();
void PrintDescriptions();
bool ItemToPurchase CheckCart(string name);
private:
string customerName = "none";
string cartDate = "January 1, 2016";
vector<ItemToPurchase> itemsInCart;
};
#endif //INC_8_23_MAIN_LAB_8_SHOPPINGCART_H
|
ShoppingCart.cpp file
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
|
#include "ShoppingCart.h"
#include "ItemToPurchase.h"
#include <iostream>
#include <ios>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <vector>
#include <cstring>
#include <fstream>
#include <sstream>
using namespace std;
void ShoppingCart::SetCustomerName(string name) {
customerName = name;
}
void ShoppingCart::SetCartDate(string date) {
cartDate = date;
}
string ShoppingCart::GetCustomerName() const {
return customerName;
}
string ShoppingCart::GetCartDate() const {
return cartDate;
}
void ShoppingCart::AddItem(class ItemToPurchase item) {
itemsInCart.push_back(item);
}
bool ItemToPurchase ShoppingCart::CheckCart(string name) {
for (int i = 0; i < itemsInCart.size(); ++i) {
if(itemsInCart.at(i).GetName() == name){
return itemsInCart.at(i);
//return false;
}
}
//return true;
return ItemToPurchase();
}
|
Part of my code that I need help on:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
bool ItemToPurchase ShoppingCart::CheckCart(string name) {
for (int i = 0; i < itemsInCart.size(); ++i) {
if(itemsInCart.at(i).GetName() == name){
return itemsInCart.at(i);
//return false;
}
}
//return true;
return ItemToPurchase();
}
|