下面是我的超市开票和股票编辑程序类的基本结构代码:
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);
友元函数的定义:
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;
}
主功能:
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;
}
请告诉我上面的代码有什么问题,我应该怎么做才能纠正它,我的动机是从创建的文件中读取,并通过用户提及产品 ID 来更改或编辑要选择的项目的数据,请帮助我如何实现此功能。