samedi 27 décembre 2014

Reading multiple derived classes from a file c++

My program consists at managing a stock of products of different types. I created 3 different classes for each product derived from a base class. I implemented a variable that describes each type as a string since type_id dosent work for my compiler. In the file i am saving each line contains a product. The first string is dedicated for the type of product. From there the data will be implemented in a table as an object of the class type. I tried every possible way i know but it always reades falsly and the program bugs. Remarque: each time i use getline or seekg the program bugs. So here is my code: (Excuse the length of it but i cant spare any line in order to reproduce the error)



#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdlib.h>
#include <fstream>
using namespace std;
class Produit
{
public:
string codeABarre;
string libelle;
string cltype;
long int qte;
string tempsDeStockage;
int coutDeStockage;
int prixDachat;
string dateDentree;
string tempsMaxDeStockage;
string getcodeABarre()
{
return codeABarre;
}
Produit();
Produit(string , string ,long int ,string , int ,int , string , string );
string getLibelle();
long int getQte();
string getTempsDeStockage();
int getCoutDeStockage();
int getPrixDachat();
string getDateDentree();
string getTempsMaxDeStockage();
string getCltype();
void setcodeABarre(string);
void setLibelle(string );
void setQte(long int);
void setTempsDeStockage(string);
void setCoutDeStockage(int);
void setPrixDachat(int);
void setDateDentree(string);
void setTempsMaxDeStockage(string);
friend ostream& operator<<(ostream&, Produit*);
friend istream& operator>>(istream&, Produit&);

};
Produit::Produit(string cab, string lib,long int q,string tds, int cds,int pda, string de, string tmds)
{
codeABarre=cab;
libelle=lib;
qte=q;
tempsDeStockage=tds;
coutDeStockage=cds;
prixDachat=pda;
dateDentree=de;
tempsMaxDeStockage=tmds;
}
Produit::Produit()
{

}
ostream& operator<<(ostream& flux,Produit* p)
{
flux<<p->getCltype()<<" "<<p->getCltype()<<" "<<p->getcodeABarre()<<" "<<p->getLibelle()<<" "<<p->getQte()<<" "<<p->getTempsDeStockage()<<" "<<p->
getCoutDeStockage()<<" "<<p->getPrixDachat()<<" "<<p->getDateDentree()<<" "<<p->getTempsMaxDeStockage()<<endl;
return flux;
}
istream& operator>>(istream& flux, Produit& p)
{
flux>>p.cltype>>p.codeABarre>>p.libelle>>p.qte>>p.tempsDeStockage>>p.coutDeStockage>>p.prixDachat>>p.dateDentree>>p.tempsMaxDeStockage;
return flux ;
}
class Souris : public Produit{
string marque;
int dpi;
string type;
public :
Souris();
Souris(string , string ,long int ,string , int ,int , string , string ,string ,int , string );
string getMarque()
{
return marque;
};
int getDpi();
string getType();
void setMarque(string);
void setDpi(int);
void setType(string);
void affichesr(Souris*);
friend ostream& operator<<(ostream&,Souris*);
friend istream& operator>>(istream&,Souris&);


};
Souris::Souris( string codeABarre1, string libelle1,long int qte1,string tempsDeStockage1, int coutDeStockage1,
int prixDachat1, string dateDentree1, string tempsMaxDeStockage1,string marque1,int dpi1,
string type1):Produit(codeABarre1,libelle1,qte1, tempsDeStockage1, coutDeStockage1, prixDachat1
,dateDentree1, tempsMaxDeStockage1)
{
marque=marque1;
dpi=dpi1;
type=type1;
cltype="Souris";
}
Souris::Souris()
{
cltype="Souris";
}
ostream& operator <<(ostream& flux,Souris* p)
{
flux<<p->getCltype()<<" "<<p->getCltype()<<" "<<p->getcodeABarre()<<" "<<p->getLibelle()<<" "<<p->getQte()<<" "<<p->getTempsDeStockage()<<" "<<p->
getCoutDeStockage()<<" "<<p->getPrixDachat()<<" "<<p->getDateDentree()<<" "<<p->getTempsMaxDeStockage()<<" "<<p->
getMarque()<<" "<<p->getDpi()<<" "<<p->getType()<<endl;

return flux;
}
istream& operator>>(istream& flux,Souris& p)
{
flux>>p.cltype>>p.codeABarre>>p.libelle>>p.qte>>p.tempsDeStockage>>p.coutDeStockage>>p.prixDachat>>p.dateDentree>>p.tempsMaxDeStockage>>p.marque>>p.dpi>>p.type;

return flux;
}
class PC : public Produit
{
string marque;
int hdd;
int ssd;
string processor;
int vram;
int ram;
string vcard;
string type;
public:
PC();
PC(string , string ,long int ,string , int ,int , string , string,string,int,int,string,int,int,string,string);
string getMarque()
{
return marque;
}
int getHdd();
int getSsd();
string getProcessor();
int getVram();
int getRam();
string getVcard();
string getType();
void setMarque(string);
void setHdd(int);
void setSsd(int);
void setProcessor(string);
void setVram(int);
void setRam(int);
void setVcard(string);
void setType(string);
void affichepc(PC*);
friend ostream& operator<<(ostream&,PC*);
friend istream& operator>>(istream&,PC&);
};
PC::PC(string codeABarre1, string libelle1,long int qte1,string tempsDeStockage1, int coutDeStockage1,int prixDachat1,
string dateDentree1, string tempsMaxDeStockage1,string marque1,int hdd1,int ssd1,string processor1,int vram1,
int ram1,string vcard1,string type1):Produit(codeABarre1,libelle1,qte1, tempsDeStockage1,coutDeStockage1, prixDachat1,
dateDentree1, tempsMaxDeStockage1)
{
marque=marque1;
hdd=hdd1;
ssd=ssd1;
processor=processor1;
vram=vram1;
ram=ram1;
vcard=vcard1;
type=type1;
cltype="PC";
}
ostream& operator <<(ostream& flux,PC* p)
{
flux<<p->getCltype()<<" "<<p->getCltype()<<" "<<" "<<p->getcodeABarre()<<" "<<p->getLibelle()<<" "<<p->getQte()<<" "<<p->getTempsDeStockage()
<<" "<<p->getCoutDeStockage()<<" "<<p->getPrixDachat()<<" "<<p->getDateDentree()<<" "<<p->getTempsMaxDeStockage()<<" "<<p->
getMarque()<<" "<<p->getHdd()<<" "<<p->getSsd()<<" "<<p->getProcessor()<<" "<<p->getRam()<<" "<<p->getVcard()<<" " <<p->
getVram()<<" "<<p->getType()<<endl;
return flux;
}
istream& operator>>(istream& flux,PC& p)
{
flux>>p.cltype>>p.codeABarre>>p.libelle>>p.qte>>p.tempsDeStockage>>p.coutDeStockage>>p.prixDachat>>p.dateDentree>>p.tempsMaxDeStockage>>p.marque>>p.hdd>>p.ssd>>p.processor>>p.ram>>p.vcard>>p.vram>>p.type;
return flux;
}
class ProduitAlimentaire: public Produit
{
string type;
string dlc;
public:
ProduitAlimentaire();
ProduitAlimentaire (string , string ,long int ,string , int ,int , string , string ,string,string);
string getType()
{
return type;
};
void setType(string);
string getDLC();
void setDLC(string);
void affichepa(ProduitAlimentaire*);
friend ostream& operator<<(ostream&,ProduitAlimentaire*);
friend istream& operator>>(istream&,ProduitAlimentaire&);

};
ProduitAlimentaire::ProduitAlimentaire( string codeABarre1, string libelle1,long int qte1,string tempsDeStockage1, int coutDeStockage1,int prixDachat1, string dateDentree1, string tempsMaxDeStockage1,string type1,string dlc1)
:Produit(codeABarre1,libelle1,qte1, tempsDeStockage1, coutDeStockage1, prixDachat1, dateDentree1, tempsMaxDeStockage1)
{
type=type1;
dlc=dlc1;
cltype="ProduitAlimentaire";
}
ProduitAlimentaire::ProduitAlimentaire()
{
cltype="ProduitAlimentaire";
}
ostream& operator <<(ostream& flux,ProduitAlimentaire* p)
{
flux<<p->getCltype()<<" "<<p->getcodeABarre()<<" "<<p->getLibelle()<<" "<<p->getQte()<<" "<<p->getTempsDeStockage()
<<" "<<p->getCoutDeStockage()<<" "<<p->getPrixDachat()<<" "<<p->getDateDentree()<<" "<<p->getTempsMaxDeStockage()<<" "
<<p->getType()<<" "<<p->getDLC()<<endl;
return flux;
}
istream& operator>>(istream& flux,ProduitAlimentaire& p)
{
flux>>p.cltype>>p.codeABarre>>p.libelle>>p.qte>>p.tempsDeStockage>>p.coutDeStockage>>p.prixDachat>>p.dateDentree>>p.tempsMaxDeStockage>>p.type>>p.dlc;
return flux;
}
int main()
{
system("color F1");
int choix;
string num,add;
cout<<"Tapez le numero du depot :"<<endl;
cin>>num;
cout<<"Tapez l'addresse du depot : "<<endl;
cin>>add;
Depot depo(num,add);
ifstream fi("Produits.txt",ios::in);
if (fi.is_open())
{
do
{
string type;
fi>>type;
if(type=="ProduitAlimentaire")
{
ProduitAlimentaire* pa=new ProduitAlimentaire();
fi>>(*pa);
if (pa->getcodeABarre()!="0")
{
depo.ajouterAliment(pa);
}
}
else if (type=="Souris")
{
Souris* sr=new Souris();
fi>>(*sr);
if (sr->getcodeABarre()!="0")
{
depo.ajouterSouris(sr);
}
}
else if (type=="PC")
{
PC* pc=new PC();
fi>>(*pc);
if (pc->getcodeABarre()!="0")
{
depo.ajouterPC(pc);
}
}
else
{
cout<<" can't read any type of products" <<endl;
}
}
while(!fi.eof());
}
fi.close();
ofstream fo;
fo.open("Produits.txt",ios::out|ios::trunc);
if (fo)
{
for(int i=0;i<depo.nbrProduits;i++)
{
string type=depo.lesProduits[i]->getCltype();
if(type=="ProduitAlimentaire")
{
ProduitAlimentaire* pa=(ProduitAlimentaire*)(depo.lesProduits[i]);
fo<<pa;
}
else if (type=="Souris")
{
Souris* sr=(Souris*)(depo.lesProduits[i]);
fo<<sr;
}
else if (type=="PC")
{
PC* pc=(PC*)(depo.lesProduits[i]);
fo<<pc;
}
}
}
fo.close();
return 0;
};

Aucun commentaire:

Enregistrer un commentaire