-1

I have to make a Shopping Cart program for school, and there are more moving parts in this program than I've ever had to deal with. I'm trying to figure out why the function in my main.cpp can't recognize the object that I created in main(). It keeps saying that

 the cart is not declared in the scope. 

I still have to finish building out the menu, but I can't even get it to recognize the object that gets created in main.

I can see that the object is being properly created, because it can be manipulated within the main() no problem. Furthermore, I even have a few placeholder commands in there to get it working. The thing is that the homework assignment requires the menu to be in a function.

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

void PrintMenu() 
{
    while (true) 
    {
        string choice;
        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 item's descriptions" << endl;
        cout << "o - Output shopping cart" << endl;
        cout << "q - Quit" << endl;
        cout << endl;
        cout << "Choose an option: " << endl;
        cin >> choice;
        if (choice == "a")      {
            cart.GetDate();
        }
        else if (choice == "d") {
        }
        else if (choice == "c") {
        }
        else if (choice == "i") {
        }
        else if (choice == "o") {
        }
        else if (choice == "q") {
            break;
        }
        else {
            cout << "That is not a valid choice" << endl;
        }
    }
}

int main()
{
    string name;
    string date;
    cout << "Enter customer's name: " << endl;
    cin >> name;
    cout << "Enter today's date: " << endl;
    cin >> date;
    cout << endl;
    cout << "Customer name: " << name << endl;
    cout << "Today's date: " << date << endl;
    ShoppingCart cart(name, date);
    ItemToPurchase apple("apple", 1, 4, "apple");
    cout << cart.GetDate();
    cart.AddItem(apple);
    cout << cart.GetNumItemsInCart();
    PrintMenu();
}
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H

#include <string>
#include <iostream>
#include <vector>
#include "ItemToPurchase.h"

using namespace std;

class ShoppingCart
{
   private:
       string customerName = "none";
       string currentDate = "January 1, 2016";
       vector<ItemToPurchase> cartItems;
   
   public:
   ShoppingCart(string name, string date);
      string GetCustomerName() const;
      string GetDate();
      void AddItem(ItemToPurchase);
      void RemoveItem(string);
      void ModifyItem();
      int GetNumItemsInCart();
      double GetCostofCart();
      void PrintTotal();
      string PrintDecriptions();         
};
#endif
#include <iostream>
#include <string>
#include <vector>
#include "ShoppingCart.h"
using namespace std;

ShoppingCart::ShoppingCart (string name, string date){
    customerName=name;
    currentDate= date;
}

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

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

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

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

}

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

double ShoppingCart::GetCostofCart()
{
    double sum = 0.0;
    for (int i = 0; i < cartItems.size(); i++)
    {
        sum += cartItems[i].GetQuantity() * cartItems[i].GetPrice();   
    }
    return sum;
}
#include "ItemToPurchase.h"

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

void ItemToPurchase::SetPrice(int SetItemPrice){
    itemPrice = SetItemPrice;    
}

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

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

int ItemToPurchase::GetPrice() const {
   return itemPrice;
}

int ItemToPurchase::GetQuantity() const {
    return itemQuantity;
}
#include <string>
#include <iostream>
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
using namespace std;

class ItemToPurchase
{
    public: 
    ItemToPurchase(string a, int b, int c, string d)
    {itemName = a;
    itemPrice = b;
    itemQuantity = c;
    itemDescription =d;
    }
        void SetName(string SetItemName);
        void SetPrice(int SetItemPrice); 
        void PrintItemDescription(); 
        void SetQuantity(int SetItemQuantity);
        string GetName() const;
        int GetPrice() const;
        int GetQuantity() const;
        void SetDescription() const;
        string GetDiscription() const;
        void PrintItemCost() const; 
    private:
        string itemName;
        int itemPrice;
        int itemQuantity; 
        string itemDescription;    
};

#endif
JeJo
  • 30,635
  • 6
  • 49
  • 88
Eli
  • 19
  • 2
  • 2
    There's a lot of code to plough through here, I'm not sure you're going to get any help. I suggest starting with something simpler, get that working, and then build things up from there. – Paul Sanders Aug 21 '21 at 20:18
  • The main issue is really coming from the way I'm working the Main() I think. I just figured I would include the whole thing in case it helped somehow – Eli Aug 21 '21 at 20:18
  • @Eli If you can get the same error (as the first error when compiling) after reducing your code to something simpler, then the whole thing does not help. The first thing I would do is put your assignment to the side, reduce `PrintMenu` to the problematic line, which I think would be `void PrintMenu(){ cart.GetDate(); }`, then reduce the rest of your code to just what you think should be needed to support this syntactically. (For example, the only member that `ShoppingCart` would need is `GetDate()`.) – JaMiT Aug 21 '21 at 21:04
  • Example of a minimized example for this error: [Function can't access variable defined in main() function](https://stackoverflow.com/questions/67653665). In that other question, the main function was reduced to defining the variable and calling the other function, and the other function was reduced to the one line trying to access that variable. All the extraneous details were removed. Couldn't make it much simpler unless you replaced the `std::string` with something more basic like `int`. – JaMiT Aug 21 '21 at 21:10

2 Answers2

2

[...], but I can't even get it, to recognize the object that gets created in main().

The main() and the PrintMenu() are two different functions with different scope. One can not know the variables from other, unless you pass or make known by any means.

In your case, you can pass the ShoppingCart object (i.e cart) from main() to the PrintMenu function, so that inside it will know which cart you meant for:

void PrintMenu(ShoppingCart& cart)
//             ^^^^^^^^^^^^^^^^^^^^
{
   // ...
}

and call the function from main() with the ShoppingCart object.

PrintMenu(cart);

That being said;

  • Please do not practice with using namespace std;. Read more: Why is "using namespace std;" considered bad practice?

  • If the member function does not modify the member, you should mark the function as const. Applies for all the getters of both ItemToPurchase and ShoppingCart classes.

  • When you return a non-trivial copyable objects like std::string in a getter, you should be avoiding copying. That means you might want to have changes like as below for all your getters which returns std::string:

    const std::string& GetDate() const /* noexcept */
    {
       return currentDate;
    }
    
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • With your last point, you need to be careful that you don't return a reference to a local variable or temporary. Here it may seem ok, as you're returning a reference to a member, but think about what happens if some other function has `return cart.GetDate();` where `cart` is a local of that fuction. – Chris Dodd Aug 22 '21 at 16:52
0

This line in PrintMenu is the problem:

cart.GetDate();

The compiler looks for something called cart within the scope of that function, which does not exist. Once way to resolve this, is to pass a reference to the cart created in main to the PrintMenu function:

void PrintMenu(ShoppingCart &cart){

and call the function like this:

PrintMenu(cart);

Note that because, in the future, you'll want to modify cart in the menu, you'll need to pass it as a reference (i.e. with &) and not as a copy or constant reference.

Yun
  • 3,056
  • 6
  • 9
  • 28