Well, I made this simple program, which is not complete yet, but any suggestions of improving code would be great
Anyways, I'm mostly posting here 'cause I need to transfer the code, now that I don't have access to my home PC :/ oh and I do know that the code should be seperated into files, but I'm too lazy to do that :P
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 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);
}
void addGatherInfo(vector<product>& inpVec){
string name;
double price;
categories category;
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;
}
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;
product::product(){
category.vegetable = false;
category.drink = false;
category.meat = false;
}
product::~product(){
}
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