Thread: Messy bunch o' code

  1. #31
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Eh? Ok now I'm lost xP
    Currently research OpenGL

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What part is it that you don't understand?

    --
    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. #33
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    You talk about many constructors :P I thought there was one for each class?
    Currently research OpenGL

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Akkernight View Post
    You talk about many constructors :P I thought there was one for each class?
    No, you can have several, just like you can overload any other function in C++ with different argument types/counts.
    So, for example, you can have:
    Code:
    Product::Product(string name, double price, categories category)
    {
    ...
    }
    (Note that you should not use the same variable name in the constructor parameters as you do for the member variables, but I'm too lazy to provide new names here).

    --
    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. #35
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    *UPDATE*
    Well, I think this is what you meant?

    main.cpp
    Code:
    #include <iostream>
    #include "product.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);
    string categoryToString(const categories& check);
    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 exit the program? Type: exit" << endl;
    	cin>> input;
    	if(input == "add"){
    		addGatherInfo(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);
    }
    
    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!";
    }
    
    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);
    
    }
    product.h
    Code:
    #ifndef PRODUCT_H
    #define PRODUCT_H
    
    #include "main.h"
    
    class product
    {
    public:
    
    	product();
    	~product();
    
    	std::string name;
    	double price;
    	categories category;
    
    protected:
    
    
    };
    
    #endif
    Last edited by Akkernight; 01-23-2009 at 06:46 AM.
    Currently research OpenGL

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Could you edit your post to mark (with for example red colour) what parts of your code has changed from the last post?

    --
    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. #37
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Sorry! Nothing got changed, I forgot to edit it >.<!
    Currently research OpenGL

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so that was what me and LaserLight discussed a bit about - you do not REALLY want to deal with the product list vector inside the product class - it shouldn't know anything about itself being stored in a vector.

    You should, however, make a constructor that takes the name, price and category information (and please look up enum's to see how you can make a better category type).

    --
    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. #39
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ok, I'll get to that, but now I got into another problem xP
    I want to read from a file called products.txt, now how do I get only the info, like get the name into one string, the price into a double and the category, into my category type?
    I've tried loads, until I just gave up, and erased all the ifstream stuff xP
    I still got the ofstream tho
    Code:
    void writeToFile(const vector<product> vec){
    
    	std::ofstream file("products.txt", ios::app);
    
    	for(vector<product>::size_type i = 0; i != vec.size(); ++i){
    		file<< vec[i].name << ";";
    		file<< vec[i].price << ";";
    		file<< categoryToString(vec[i].category) << "\n";
    	}
    
    	file.close();
    
    }
    Currently research OpenGL

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