Need Help with a Pointer

Pages: 12
You asked if I am allowed to rename any variables, the answer is no.


I would have thought that you could have control over the variables names in the cpp files, but leave the ones in the header files intact.
My point about the confusion in the names is this: Here are 3 variables names for the same thing - idCode, id, pID. The first 2 are in the header file, which you don't have control over. The last one you should have control over, I would make it ProductIdCodeArg in the cpp file parameter if it was in the constructor, or just ProductIdCode if in a normal function .

With the names in the header file, it's not good to pre-pend each member with the class name, but IMO it does make sense to use a longer meaningful name for the function parameters and use something similar in the cpp file, because the header file is what the client sees. The client is the coder who includes the header file as part of an API and makes the function call. It's awkward here because you are the one writing the cpp file.

So IMO, id is not a good name for a parameter in the header file. If I was the user, I would immediately question id for what - lots of things have id's? For example, in

1
2
Product* Store::getProductFromID(std::string idCode)
{


So there, the parameter name is the same as the class member name, but I don't know what it means: I shouldn't have to guess, or even go looking somewhere for it.

Given all that, I would ask your question again in detail to your teachers.


The bad news: I'm having a hard time with my productSearch function


115
116
117
118
119
120
121
122
void Store::productSearch(std::string prodName)
{
    bool keyWord = false;
    
    for (std::size_t search = 0; search < inventory.size(); search++)
    {
        for (int x = 0; x < 1; x++) // this only runs once
        {
Last edited on
Your productSearch function looks to complicated. I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//search through the inventory vector for a product based on a keyword search. 
// Both the title and description of the product shoud be searched
void Store::productSearch(std::string str)
{
  bool found = false;
  for (size_t index = 0; index < inventory.size(); index++)
  {
    Product* product = inventory[index];
    if ( /* your search logic here */)
    {
      found = true;
      cout << "ID Code: " << product->getIdCode() << endl;
      cout << "Price: $ " << product->getPrice() << endl;
      cout << " " << product->getDescription() << endl;
    }
  }
  if (!found)
    cout << "** No products found for " << str << "\n";
}
More good news, I have almost everything up and running except for the checkOutMember function. I am having trouble with a dereferenced pointer and I'm not sure how to declare it first. The problem seems to be coming from any/all of the pointers in this function...
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
void Store::checkOutMember(std::string mID)
{
    Customer* currentCustomer = nullptr;
    Product* currentItem = nullptr;
    std::vector<std::string> currentCart = currentCustomer->getCart();
    int x = currentCart.size();
    int memberIndex = -1;
    double shipping = 0.0;
    double total = 0.0;
    
    if (currentCustomer == nullptr)
    {
        currentCustomer = getMemberFromID(mID);
    }
    
    if (currentItem == nullptr)
    {
        currentItem = getProductFromID(currentCart[x]);
    }
    
    //loop through the member vector to see if this is a store member
    for (std::size_t index = 0; index < members.size(); index++)
    {
        if (members[index]->getAccountID() == mID)
        {
            memberIndex = index;
            break;
        }
    }
    
    //if the member is not found, display that message
    if (memberIndex == -1)
    {
        cout << " Member # " << mID << " not found." << endl;
        return;
    }
   
    //display the name and price of the product(s) in the cart
    for (int y = 0; y < x; y++)
    {
        int itemAvailable = currentItem->getQuantityAvailable();
        std::string idCode = currentItem->getIdCode();
        std::string title = currentItem->getTitle();
    
    if (itemAvailable == 0) //if out of stock
    {
        cout << "Sorry, product # " << inventory[memberIndex]->getIdCode() << " " <<
        inventory[memberIndex]->getTitle() << " is no longer available." << endl;
    }
    
    //add the price of all items in the cart and return the total as the subtotalelse
    {
        double price = currentItem->getPrice();
        cout << title << "  -  $ " << price << endl;
        currentItem->decreaseQuantity();
        total += price;
    }

    cout << "Subtotal: " << total << endl;
   
   //if the customer is a premium member, the shipping is free
    if (currentCustomer->isPremiumMember())
    {
        shipping = 0;
    }
    //if the member is not a premium member, the shipping is 7% of the subtotal
    else
        shipping = total * 0.07;
    
    //display the shipping total
    cout << "Shipping cost: $ " << shipping << endl;
   
   //display the grand total
    cout << "Total: $ " << total + shipping << endl;

    members[memberIndex]->emptyCart();
    }
}

As for my other issues, I rewrote the productSearch function and have that functioning properly, same with addProductToMemberCart.

My question must not have been correctly phrased to the instructor or I misunderstood the answer regarding renaming variables in my .cpp files. After I get full functionality I'll go back through and make the code more readable.

Thank you!
Last edited on
1
2
Customer* currentCustomer = nullptr;
std::vector<std::string> currentCart = currentCustomer->getCart();

You can't use a nullptr unless you want to crash your program.
You should search for the currentCustomer by its id and if found you can use it.
1
2
3
4
5
6
7
Customer* currentCustomer = getMemberFromID(mID);
if (currentCustomer != nullptr)
  // go ahead
else
{
   // handle customer not found
}
Just the nullptr thing, I notice you have checks for that in various places, just wondering if your vectors are going to have any nullptr in them. In other words only put valid items into the vectors. :+)

Edit:

However returning nullptr as a way of saying something isn't found is a reasonable idea.

We seem to have lost your code for Store.cpp

As for my other issues, I rewrote the productSearch function and have that functioning properly, same with addProductToMemberCart.


Might be an idea to post them again, just because it works - doesn't make it right :+)

Variable names again:

x is not a good variable name, how about CurrentCartSize ?

After I get full functionality I'll go back through and make the code more readable.


The other way around would be better, who knows what problems will be fixed by doing that. Use the re-factoring functions in your IDE, so you don't miss any and make more problems :+)
Last edited on
Topic archived. No new replies allowed.
Pages: 12