Thread: Messy bunch o' code

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    Surely you mean that addProd would be the member of an object that holds the vector of products?
    No, I really meant that it should become a member function of product. It should become a constructor that takes a name, price and category. There is also a part that should become the member function of another container of product objects, but that's a slightly different story.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    No, I really meant that it should become a member function of product. It should become a constructor that takes a name, price and category. There is also a part that should become the member function of another container of product objects, but that's a slightly different story.
    Hmm. Either I've completely misunderstood, or you are confusing addProd with addGatherInfo...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    Hmm. Either I've completely misunderstood, or you are confusing addProd with addGatherInfo...
    I originally did not think of suggesting a new class, so my suggestion was motivated more by the fact that the function was accessing member variables to initialise the brand new product object rather than the fact that it was actually adding a product to a container (which admittedly should be its primary purpose given its name).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    I originally did not think of suggesting a new class, so my suggestion was motivated more by the fact that the function was accessing member variables to initialise the brand new product object rather than the fact that it was actually adding a product to a container (which admittedly should be its primary purpose given its name).
    Yes, I guess that there are two different parts of this - and I had actually misunderstood a bit of what you said as well - addProd does TWO things - it "constructs" the product object by assigning the member variables - that should really be done as a constructor.

    It also adds the product to a vector. Now, it would probably be a good thing to have a "product containter" that would have the addProd (add product to the vector), listProd() (whcih is currently a for-loop in the menu interpreter) and removeProd() [and probably a few other ones, e.g. "find product", "save product list to file", "load product list from file", and so on].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Yeah well, I dunno how to work with classes yet xP
    Currently research OpenGL

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Akkernight View Post
    Yeah well, I dunno how to work with classes yet xP
    But that is why you are doing this exercise, right?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    No... Did it just to code something, and I learned loads, like I didn't know I could use a struct declaration inside another structs xP but hey... Maybe you're right, maybe I should learn Classes... Off to Cprogramming.com !
    Currently research OpenGL

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Akkernight View Post
    No... Did it just to code something, and I learned loads, like I didn't know I could use a struct declaration inside another structs xP but hey... Maybe you're right, maybe I should learn Classes... Off to Cprogramming.com !
    Well, you are after all programming in C++, which was originally called "C with Classes", which may be a better indication of "how you should solve things".

    Programming is a lot of learning better ways to do things, and the best way to learn that is "to do" it in the first place. Then ask someone who knows more to check if there is a better way to do it, and then use that new knowledge to make your code better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    One question, Imma code the class in another .cpp file caleld product.cpp, how do I make the .h file? Like what do I include in it?
    When you make functions you add the prototype, how does it work with classes? Do I just type in class Product ? Or do I need to type in the class functions too?
    Currently research OpenGL

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Akkernight View Post
    One question, Imma code the class in another .cpp file caleld product.cpp, how do I make the .h file? Like what do I include in it?
    When you make functions you add the prototype, how does it work with classes? Do I just type in class Product ? Or do I need to type in the class functions too?
    The header file should contain the declaration of the class, e.g.
    Code:
    #ifndef MYCLASS_H
    #define MYCLASS_H
    class MyClass
    {
       private:
          int x;
       public:
          MyCalss(int ax);
          int GetX(void);
          void SetX(int ax);
    };
    #endif
    The class source file then contains the definitions of those functions:
    Code:
    #incldue "myclass.h"
    MyClass::MyClass(int ax): x(ax) 
    {
    }
    
    int MyClass::GetX(void)
    {
        return x;
    }
    
    void MyClass::SetX(int ax)
    {
       x = ax;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ok thanks!
    Currently research OpenGL

  12. #27
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    *UPDATE*
    Well that's not why I'm posting, but the code has been updated, and again, I'm also using this forum for transfer purposes, hope nobody minds :P
    But I don't know much about classes, so could someone please help me with the class? The code compiles, it's just that I'm not taking full advantage of the class, so could someone tell me what else to put into it? Something that takes full advantage of the class?
    Thanks in advance!
    Currently research OpenGL

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you update the code, you should really post it in a new post, since editing the old one with the new code like that makes comments that didn't quote the original code look weird [because the code is no longer there] - not to mention that it makes it hard to see the new code when posting a reply, since it's more than 14 posts back, so I need to click on the "here" to see the whole thread.

    The problem compiling your code is that you are using categories in the product class, but you haven't declared what categories are. You can either move that into the product header file, or make it into it's own header file.

    I also suggested that you use an enum, assuming you are not planning to have something that is both meat and drink at the same time (or similar).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    The code does compile o.O And here's the code...
    I just wanna learn how to take advantage of the class :P
    EDIT: Oh and enums are strange :P don't understnad them xD

    main.cpp
    Code:
    #include <iostream>
    #include "product.h"
    #include "fileIO.h"
    
    using std::cout;	using std::endl;
    using std::vector;	using std::string;
    using std::cin;
    
    void addProd( const string name, const double price, const categories category,
    			 vector<product>& vec);
    void addGatherInfo(vector<product>& prodVec);
    void prodRemove(string name, vector<product>& vec);
    
    int main(){
    	bool run = true;
    	vector<product> prodVec;
    	while(run == true){
    
    	string input;
    	cout<< "\nWant to add a new product? Type: add" << endl;
    	cout<< "Want to see a list of added products? Type: list" << endl;
    	cout<< "Want to remove a product? Type: remove" << endl;
    	cout<< "Want to save? Type: save" << endl;
    	cout<< "Want to exit the program? Type: exit" << endl;
    	cin>> input;
    	if(input == "add"){
    		addGatherInfo(prodVec);
    	}
    
    	if(input == "save"){
    		writeToFile(prodVec);
    	}
    
    	if(input == "list"){
    		for(vector<product>::size_type i = 0; i != prodVec.size(); ++i){
    
    			cout<< "\nProduct name: " << prodVec[i].name;
    			cout<< "\nProduct price: " << prodVec[i].price;
    			cout<< "\nProduct category: " << categoryToString(prodVec[i].category);
    			cout<< "\n----------------\n";
    		}
    	}
    
    	if(input == "remove"){
    		string name;
    		cout<< "Please type in a name for the product: ";
    		cin>>name;
    		prodRemove(name, prodVec);
    	}
    
    	
    
    	if(input == "exit"){
    		run = false;
    	}	
    	
    	}
    	return 0;
    }
    
    
    void addGatherInfo(vector<product>& inpVec){
    
    		string name;
    		double price;
    		categories category;
    		product prod;
    
    		cout<< "Please type in a name for the product: ";
    		cin>> name;
    		cout<< "\nPlease type in a price for the product: ";
    		cin>> price;
    		cout<< "\nPlease choose a category for the product: " <<endl;
    		cout<< "1. vegetables\n" << "2. drink\n" << "3. meat\n";
    		cout<< "Type in one of the numbers for those categories: ";
    		int choNum;
    		cin>> choNum;
    		switch(choNum){
    			case 1:
    				category.vegetable = true;
    				break;
    			case 2:
    				category.drink = true;
    				break;
    			case 3:
    				category.meat = true;
    				break;
    		}
    	
    		prod.addProd(name, price, category, inpVec);
    }
    
    void prodRemove(string name, vector<product>& vec){
    	for(vector<product>::size_type i = 0; i != vec.size(); ++i){
    		if(vec[i].name == name){
    			vec.erase(vec.begin() + i);
    			break;
    		}
    	}
    }
    main.h
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    
    #include <string>
    #include <vector>
    
    struct categories{
    	bool vegetable;
    	bool drink;
    	bool meat;
    };
    
    /*struct product {
    	std::string name;
    	double price;
    	categories category;
    };*/
    
    #endif
    product.cpp
    Code:
    #include "product.h"
    
    using std::string; using std::vector;
    
    product::product(){
    	category.vegetable = false;
    	category.drink = false;
    	category.meat = false;
    }
    
    product::~product(){
    }
    
    void product::addProd( const string name, const double price, const categories category,
    						vector<product>& vec) {
    
    				product prod;
    				prod.name = name;
    				prod.price = price;
    				prod.category = category;
    
    				vec.push_back(prod);
    
    }
    
    string categoryToString(const categories& check){
    	if(check.vegetable == true)
    		return "Vegetable";
    	if(check.drink == true)
    		return "Drink";
    	if(check.meat == true)
    		return "Meat";
    
    	return "Product category not defined!";
    }
    product.h
    Code:
    #ifndef PRODUCT_H
    #define PRODUCT_H
    
    #include "main.h"
    
    std::string categoryToString(const categories& check);
    
    class product
    {
    public:
    
    	product();
    	~product();
    
    	std::string name;
    	double price;
    	categories category;
    
    	void addProd( const std::string name, const double price, const categories category,
    		std::vector<product>& vec);
    
    protected:
    
    
    };
    
    #endif
    Last edited by Akkernight; 01-23-2009 at 09:03 AM.
    Currently research OpenGL

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	category.vegetable = false;
    	category.drink = false;
    	category.meat = false;
    This probably should be in the constructor for a category.

    And you need to add a constructor that does this:
    Code:
    				prod.name = name;
    				prod.price = price;
    				prod.category = category;
    when you do:
    Code:
        product prod(name, price, category);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM