Below is the basic structure code for my super market billing and stock editing program class:
class Admin
{
public:
admin();
private:
int Pid;
char name[20];
double quant;
double price;
double disc;
double net_price;
friend istream &read(istream&, Admin&);
friend ostream &show(ostream&, const Admin);
};
istream &read(istream&, Admin&);
ostream &show(ostream&, const Admin);
definition of friend functions:
istream &read(istream &is, Admin &commodity)
{
double dis;
fflush(stdin);
is>>commodity.Pid;
is.getline(commodity.name,30);
is>>commodity.quant
>>commodity.price
>>commodity.disc;
dis=grs_pr*(commodity.disc/100);
commodity.net_price=grs_pr-dis;
return is;
}
ostream &show(ostream &os, const Admin thing)
{
os << thing.name <<" " << thing.quant <<" "
<< thing.price <<" " << thing.disc <<" "
<< thing.net_price << endl;
return os;
}
Main function:
int main()
{
admin item;
while(read(cin,item)
{
ofstream file;
file.open("Stock.dat",ios::binary | ios::app);
file.write(reinterpret_cast<const char*>(&item),sizeof(Admin));
}
ifstream readFile("Stock.dat",ios::in|ios::binary);
while(!readFile.eof())
{
readFile.read(reinterpret_cast<char*> (&item),sizeof(Admin));
Admin readedItem;
read(readFile,readedItem); /*i have used read and show function to watch the values that are read by readFile but every time show function output some five exponential values like 5.23689e-301*/
show(cout,readedItem);
}
return 0;
}
Please tell me what is wrong in above code and what should i do to correct it, my motive is to read from the file created and to alter or edit the data of the item that is to be selected by mentioning the product ID by the user, please help me how to attain this functionality.
main(), you havewhile(read(cin,item) {. This is simply wrong, missing a parenthesis. Your code should be an SSCCE (Short, Self-Contained, Correct Example). Amongst other things, that means it should compile!