Having trouble getting making a mutator return a boolean value

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();
}

Last edited on
1
2
3
4
5
6
7
8
9
bool ItemToPurchase ShoppingCart::CheckCart( const std::string& name ) {
    
    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    for( const auto& item : itemsInCart ) 
        if( item.GetName() == name ) return true ;
    
    return false ;
}


Ideally, make it const member function:
bool ItemToPurchase ShoppingCart::CheckCart( const std::string& name ) const
https://isocpp.org/wiki/faq/const-correctness#const-member-fns
Last edited 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 false;
            //return false;

        }

    }
    //return true;
    return true;
}


If you want to return a true or false then simply return it? As long as the condition - itemsInCart.at(i).GetName() == name actually works, then simply use the if statements to determine whether to return true or false.
PigeonGiraffe wrote:
bool ItemToPurchase ShoppingCart::CheckCart(string name)

What do you want to return, ItemToPurchase or bool?
@ JLBorges

I did this and I get two errors.
Error #1:
I have already stated this error but its the:

"Expected ';' after top level declarator"

between "ItemToPurchase(Right here)ShoppingCart::CheckCart"

Error #2:
This one is in my .h file. When putting bool in front of my mutator, my vector brings up the error:

"Invalid use of non-static data member 'ItemToPurchase'"

--------------------------------------------------------------------------------------

@zapshe

I tried that already and I get the error right between:

"ItemToPurchase(Right here)ShoppingCart::CheckCart" that says:

"Expected ';' after top level declarator".

--------------------------------------------------------------------------------------

@Enoizat

I would like to return a bool. I had it set up to return ItemToPurchase to test that it is working by cout-ing on the console. I am now trying to manipulate it so that it returns a bool.

--------------------------------------------------------------------------------------

Thank you all for responding. Is there anything else you might want to see that might help?
Correction:

1
2
3
4
5
6
7
8
9
10
// bool ItemToPurchase ShoppingCart::CheckCart( const std::string& name ) {
bool ShoppingCart::CheckCart( const std::string& name ) {
    
    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    for( const auto& item : itemsInCart ) 
        if( item.GetName() == name ) return true ;
    
    return false ;
}
@JLBorges

That was it! It worked! Thank you so much! This was the only thing holding my back from finishing this project.
Topic archived. No new replies allowed.