Eh? Ok now I'm lost xP
Printable View
Eh? Ok now I'm lost xP
What part is it that you don't understand?
--
Mats
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:
(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).Code:Product::Product(string name, double price, categories category)
{
...
}
--
Mats
*UPDATE*
Well, I think this is what you meant?
main.cpp
main.hCode:
#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;
}
}
}
product.cppCode:#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.hCode:#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);
}
Code:#ifndef PRODUCT_H
#define PRODUCT_H
#include "main.h"
class product
{
public:
product();
~product();
std::string name;
double price;
categories category;
protected:
};
#endif
Could you edit your post to mark (with for example red colour) what parts of your code has changed from the last post?
--
Mats
Sorry! Nothing got changed, I forgot to edit it >.<!
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
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();
}