Code:#include "stdafx.h" #include "product.h" int product::pricemid = 0; int product::number_of_products = 0; int _tmain(int argc, _TCHAR* argv[]) { int prod = 0; int a = 0,b = 0,c = 0,d = 0,e = 0,f = 0; product *p = new product [100]; p[0].set_it (20,"zwanzigeuroschein",100,"Geld"); p[1].set_it (40,"zweizwanzigeuroschein",100,"Geld"); p[2].set_it (60,"dreizwanzigeuroschein",100,"Geld"); for (int i=0; i != 1; ) { char *name1 = new char[40] ; char *name2 = new char [40]; char *name3 = new char [40]; cout << "was willst du tun?" << endl; cin >> name1; cout << endl; cout << "was ist der preis? (nur eine zahl)" << endl; cin >> a; cout << " was ist der name des Produktes? " << endl; cin >> name2; cout << "wie hoch ist die Produktqualität? " << endl; cin>> b; cout << " was ist der Produkttyp?" << endl; cin >> name3; p[prod].set_it (a,name2,b,name3); cout << "1 für ende 0 für weiter"; cin >> i; p[prod].printprice(); prod++; if (prod >1) {p[(prod - 1)].printprice();} name1 = NULL; delete [] name1; delete [] name2; //name2 = NULL; delete [] name3; //name3 = NULL; } cin >> a; return 0; }
and
Code:#include <iostream> using namespace std; class product { public : char *name ; char *type; int price; int quality; int brand_recognition; int brand_loyalty; int player; //factory * manufacturer; int identifier; //SHOULD BE DIFFRENT FOR DIFFERENT PRODUCTS BUT ISNT THIS WAY static int pricemid ; static int number_of_products; void setprice (int); void printprice() ; product(int prc = 0,char *nm = "product" , int qlty = 0, char *type = "nix"); void set_it(int prc = 0,char *nm = "product" , int qlty = 0, char *type = "nix"); ~product(); }; void product::setprice (int prc) { this -> price = prc; }; void product::printprice () { cout << "der Preis des Produktes" << this -> name << " ist " << this->price << endl; cout << " es ist vom typ " << this ->type << endl; }; product::product (int prc, char *nm, int qlty, char *type) { this -> name = new char[strlen(nm)]; this -> type = new char[strlen(type)]; this -> price = prc; //this -> name = nm; //instead: strcpy(this->name , nm); this ->quality = qlty; //this -> type = type; strcpy(this->type,type); this->number_of_products++; }; void product::set_it(int prc ,char *nm , int qlty , char *type ) { int i, o; this -> name = new char[strlen(nm)] ; this -> type = new char[strlen(type)]; this -> price = prc; //this -> name = nm; strcpy(this->name , nm); this ->quality = qlty; //this -> type = type; strcpy(this->type,type); this->number_of_products++; for (i=0;i < number_of_products; i++) { o = 1; } }; product::~product () { delete[] this -> name; this->name = NULL; delete[] this -> type; this->type = NULL; };
Problems:
without the name1 = NULL; at the end of the loop I get an error about a failed asses.
Also p[prod].printprice(); and p[(prod-1)].printprice(); give the same result
THANKS




Reply With Quote