我正在建立一个库存管理系统,它刚刚启动我的程序。我对此代码有疑问。
当我启动程序并显示对象时,它的工作正常但在此之后我再次按2进行显示循环并没有中断。
#include<iostream>
#include<fstream>
using namespace std;
class products
{
char pname[20];
int pid;
int quantity;
int price;
public:
products()
{
pname[0]='\0';
pid=NULL;
quantity=0;
price=0;
}
void get()
{
cout<<"Enter Product Name: ";
cin>>pname;
cout<<"Enter Product id: ";
cin>>pid;
cout<<"Enter Product Quantity: ";
cin>>quantity;
cout<<"Enter Product Price: ";
cin>>price;
}
void display()
{
cout<<"Product Name: ";
cout<<pname;
cout<<"\nProduct id: ";
cout<<pid;
cout<<"\nProduct Quantity: ";
cout<<quantity;
cout<<"\nProduct Price: ";
cout<<price<<endl;
}
void sale()
{
quantity--;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
products p;
int c;
fstream f;
f.open("product.dat",ios::binary|ios::in|ios::out|ios::app);
do{
cout<<"Enter 1 to add product \n 2 to Display all products";
cin>>c;
switch(c)
{
case 1:
p.get();
f.write(reinterpret_cast<char*>(&p),sizeof(products));
break;
case 2:
f.seekg(0,ios::beg);
while(!f.eof())
{
f.read(reinterpret_cast<char*>(&p),sizeof(products));
p.display();
}
break;
}
}while(c!=0);
return 0;
}`