CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    Exclamation Vectors iterators

    Code:
    this is my header file
    
    #ifndef HW1_H
    #define HW1_H
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    enum SystemError
    
    {
    	ERR_NOT_ENOUGH_PRODUCT = 0,
    	ERR_PRODUCT_DOES_NOT_EXIST = 1,
    	ERR_ORDER_ALREADY_EXISTS = 2,
    	ERR_NOT_ENOUGH_BUDGET = 3,
    	ERR_CUSTOMER_DOES_NOT_EXIST= 4,
    	ERR_STORE_DOES_NOT_EXIST = 5,
    	ERR_CUSTOMER_ALREADY_EXISTS= 6,
    	ERR_STORE_ALREADY_EXISTS = 7,
    	ERR_ORDER_DOES_NOT_EXIST = 8,
    
    };
    /* Product */
    
    
    
    class Product
    {
    
    	private:
    
    		string productName;
    		string productType;
    		int productPrice;
    		int productAmount;
    
    	public:
    
    		Product(string productName, string productType, int productPrice,int productAmount);
    		Product();
    		~Product();
    		string getProductName();
    		string getProductType();
    		int getProductPrice();
    		int getProductAmount();
    		void setAmount(int newAmount);
    		void setPrice(int newPrice);
    		friend ostream& operator<<(ostream& out, const Product &product);
    };
    
    
    
    ostream& operator<<(ostream &out, const Product &product);
    /* Order */
    
    
    
    class Order
    {
    
    	private:
    
    		string orderID;
    		int orderDate;
    		string customerID;
    		string storeName;
    		vector<Product> shoppingList;
    
    	public:
    
    		Order();
    		Order(string orderID, int orderDate, string customerID, string
    		storeName);
    		~Order();
    		string getOrderID();
    		int getOrderDate();
    		string getCustomerID();
    		string getStoreName();
    		void setStore(string newStore);
    		void addProduct(Product p);
    		void removeProduct (string pname, int amount);
    		int orderTotal();
    		void listAllProducts();
    		void listProductsOfType (string t);
    		Product getProduct(int index);
    		int getProductSize();
    		friend ostream& operator<<(ostream& out, const Order &order);
    };
    
    
    
    ostream& operator<<(ostream &out, const Order &order);
    /* Store */
    
    
    
    class Store
    {
    	private:
    		string storeName;
    		string storeAddress;
    		vector<Product> availableProducts;
    	public:
    		Store();
    		Store(string storeName, string storeAddress);
    		~Store();
    		string getStoreName();
    		string getStoreAddress();
    		void setAddress(string newAddress);
    		void addProduct(Product p);
    		void removeProduct(string pname, int amount);
    		Product getProduct(int index);
    		int getProductSize();
    		void setProductAmount(int productIndex, int newAmount);
    		friend ostream& operator<<(ostream& out, const Store &store);
    };
    
    
    
    ostream& operator<<(ostream &out, const Store &store);
    /* Customer */
    
    
    
    class Customer
    {
    	private:
    		string customerID;
    		string customerName;
    		int phone;
    		int creditCardNo;
    		string passwd;
    		int budget;
    	public:
    		Customer();
    		Customer(string customerID, string customerName, int phone, int
    		creditCardNo, string passwd, int budget);
    		~Customer();
    		string getCustomerID();
    		string getCustomerName();
    		int getCustomerPhone();
    		int getCustomerCreditCardNo();
    		string getCustomerPasswd();
    		int getBudget();
    		void setPhone(int newPhone);
    		void changePasswd(string newPasswd);
    		void setBudget(int newBudget);
    		friend ostream& operator<<(ostream& out, const Customer &customer);
    };
    
    
    
    ostream& operator<<(ostream &out, const Customer &customer);
    /* Shopping Zone */
    
    
    
    class ShoppingZone
    {
    
    	private:
    
    		vector<Order> orders;
    		vector<Customer> customers;
    		vector<Store> stores;
    
    	public:
    
    		ShoppingZone();
    		~ShoppingZone();
    		void addOrder(Order o);
    		void addCustomer(Customer c);
    		void addStore(Store s);
    		Order getOrder( string orderID);
    		Customer getCustomer( string customerID);
    		Store getStore( string storeName);
    		void listAllStores();
    		void listProductsOfCustomer (string customerID);
    		void listProductInfo (string storeName, string type);
    };
    #endif

    and this is my listProductInfo func.


    Code:
    void ShoppingZone::listProductInfo (string storeName, string type)
    {
    	int i,j;
    
    	vector<Store>::iterator it;
    	vector<Product>::iterator it2;
    
    	for ( it=(this->stores.begin()) ; it < (this->stores.end()); it++  )
    	{
    		if( it->getStoreName() == storeName )
    		{
    			cout << it->availableProducts[0].getProductType();	
    			
    			/*for ( it2=(it->availableProducts.begin()) ; it2 < (this->availableProducts.end()); it2++ )
    					{cout <<"evet";}*/
    		}
    	}
    }
    how can reach any Product which exist in availableProducts vector from this function. THis cause problem because availableProducts is fixed. MOreover header file is fixed it cannot be changed. Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Vectors iterators

    Quote Originally Posted by loves_oi
    how can reach any Product which exist in availableProducts vector from this function.
    You should make use of the Store interface, i.e., call the getProduct or setProductAmount member functions of the Store object found.

    Quote Originally Posted by loves_oi
    MOreover header file is fixed it cannot be changed.
    I would change certain parts of your class definitions in the header file, because:
    • You should not have a using directive (using namespace std; ) at file scope in a header file.
    • Many cases of pass by value, especially of std::string objects, should have been changed to pass by (const) reference.
    • Many of your member functions are not const correct, e.g., getProductName should not change the Product, yet it is not declared const.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vectors iterators

    Quote Originally Posted by loves_oi View Post
    [code]
    Code:
    void ShoppingZone::listProductInfo (string storeName, string type)
    {
    	int i,j;
    
    	vector<Store>::iterator it;
    	vector<Product>::iterator it2;
    
    	for ( it=(this->stores.begin()) ; it < (this->stores.end()); it++  )
    	{
    		if( it->getStoreName() == storeName )
    		{
    			cout << it->availableProducts[0].getProductType();	
    			
    			/*for ( it2=(it->availableProducts.begin()) ; it2 < (this->availableProducts.end()); it2++ )
    					{cout <<"evet";}*/
    		}
    	}
    }
    Look at the code below.
    Code:
    #include <algorithm>
    #include <string>
    //...
    using namespace std;
    //...
    struct FindByName
    {
        std::string sName_;
        FindByName(const std::string& sName) : sName_(sName) {}
    
        bool operator()(Store& theStore) const
        {  return sName_ == theStore.getStoreName();   }
    };
    
    void DisplayName(Product& theProduct)
    {
        cout << theProduct.getProductName() << "\n";
    }
    
    void ShoppingZone::listProductInfo (const string& storeName, 
                                                             const string& type)
    {
           // Search for the store matching the store name
    	vector<Store>::iterator it = find_if(stores.begin(), stores.end(), FindByName(storeName));
    
            if ( it != stores.end())
           {
                // store exists, now display the name of each product in the store that was found
                for_each(it->availableProducts.begin(), it->availableProducts.end(), DisplayName);
          }
    }
    I leave it to you as an exercise as to why the code above works without having to code a single for loop.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured