64,281
社区成员
发帖
与我相关
我的任务
分享
int bianhaochaxun()//查询商品——按编号查询
{
cout<<"请输入要查询的商品编号!"<<endl;
int bianhao1;
int bianhao2;
cin>>bianhao2;
shangpin a(0," ",0,0);
ifstream in("f1.txt",ios::binary);
if(!in)
{
cout<<"无法打开商品库,请告知管理员\n\n"<<endl;
return 0;
}
for(int i=0;i<=2000;i++)
{
in.seekg(sizeof(shangpin)*i,ios::beg);
in.read((char *)&a,sizeof(shangpin));
bianhao1=a.geibianhao();
//cout<<"编号1:"<<bianhao1<<endl;
//cout<<"编号2:"<<bianhao2<<endl;
if(bianhao2==bianhao1)
{
if(a.geicunzai()==1)
{
if(a.geikucun()>=1)
{
cout<<"存在性"<<a.geicunzai()<<endl;
a.show();
in.close();
return 0;
}
else
{
cout<<"抱歉,该商品已售完,请联系管理员补货!以下是商品信息!"<<endl;
a.show();
in.close();
return 0;
}
}
else
{
cout<<"抱歉,您查询的商品已不存在!"<<endl;
in.close();
return 0;
}
}
else
continue;
}
cout<<"您输入的商品编号有误!请重新查询"<<endl;
in.close();
return 0;
}
int shanchushangpin() //删除商品
{
int bianhao1;
int bianhao2;
cout<<"请输入要删除商品的编号:"<<endl;
cin>>bianhao2;
shangpin a(0,"",0,0);
fstream io("f1.txt",ios::in|ios::out|ios::binary);
if(!io)
{
cout<<"无法打开商品库,请告知管理员\n\n"<<endl;
return 0;
}
for(int i=0;i<=2000;i++)
{
io.seekg(sizeof(shangpin)*i,ios::beg);
io.read((char *)&a,sizeof(shangpin));
bianhao1=a.geibianhao();
if(bianhao1==bianhao2)
{
cout<<"查询到商品信息如下:"<<endl;
a.show();
cout<<"确认要删除请按1,否则按2"<<endl;
int banduan;
cin>>banduan;
if(banduan==1)
{
a.cuncunzai(0);
cout<<"删除成功!"<<endl;
io.seekp(sizeof(shangpin)*i,ios::beg);
io.write((char *)&a,sizeof(shangpin));
io.close();
return 0;
}
else
io.close();
return 0;
}
else continue;
}
cout<<"找不到该商品,请重新输入"<<endl;
io.close();
return 0;
}
int zengjiashangpin() //增加商品
{
shangpin b(0," ",0,0);
int bianhao2;
string name2;
double jiage2;
int kucun2;
int xuanzhe;
while(1)
{
cout<<"请输入商品的编号:"<<endl;
cin>>bianhao2;
cout<<"请输入商品的名称:"<<endl;
cin>>name2;
cout<<"请输入商品的价格:"<<endl;
cin>>jiage2;
cout<<"请输入商品的进货量:"<<endl;
cin>>kucun2;
cout<<"请确认商品信息,确认请按1,重输请按2"<<endl;
cin>>xuanzhe;
if(xuanzhe==1)
{
b.cunbianhao(bianhao2);
b.cunname(name2);
b.cunjiage(jiage2);
b.cunkucun(kucun2);
cunru(b);
return 0;
}
else
{
system("cls");
continue;
}
}
}
bool FuntionTest()
{
char cBuffer[5];
strcpy(cBuffer,"Hello"); //加上结束符大小为6 大于cBuffer
return true; //释放资源的时候才发现这里溢出了
}
在程序拷贝的过程中是不会出错的,在释放资源的时候才会发现溢出了,也就报错了,