如何避免重复具有相同产品 ID 的产品
How I can avoid from duplication of products with same Product ID
如何避免在文件处理中重复具有相同产品ID的产品,请任何人都可以告诉我它的代码 ............................................................................................................................................................
struct Product
{
int Pid;
char Pname[55];
int balance;
};
void AddProduct()
{
Product p;
cout <<"nProduct ID: ";
cin>>p.Pid;
cout <<"Product Name: ";
cin>>p.Pname;
cout <<"Balance: ";
cin>>p.balance;
ofstream ofs("Products.bin");
ofs.write(reinterpret_cast<char *>(&p), sizeof(p));
ofs.close();
cout <<"nProduct Successfully Saved";
}
ProductOpt()
{
char ch;
while(1)
{
cout <<"n1. Add Product"<<endl;
cout <<"2. Display All Products"<<endl;
cout <<"3. Modify Product"<<endl;
cout <<"4. Delete Product"<<endl;
cout <<"5. Back"<<endl;
ch = getch();
cout<<endl;
if(ch == '1')
AddProduct();
else if(ch == '2')
DisplayProduct();
else if(ch == '3')
ModifyProduct();
else if(ch == '4')
DeleteProduct();
else if(ch == '5')
break;
else
cout <<"Invalid Option"<<endl;
}
}
输入后,只需先以读取模式打开文件,运行循环直到文件末尾,并使用文件中存储的 ID 检查 Id。如果匹配,则打印它是重复的 Id,然后再次调用该函数以再次输入数据。如果循环在文件末尾终止,则以写入模式打开文件并将结构数据附加到文件中。
相关文章:
- C++避免重复声明的语法是什么
- 在没有太多条件句的情况下,我如何避免被零除
- 如何重构类层次结构以避免菱形问题
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 以下示例中如何避免代码复制?C++/库达
- Python中的for循环与C++有何不同
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 是否应该在模板化代码中完全避免const
- 从函数角度看ID到文件路径的内部与外部映射
- 通过组合不同的类型来创建唯一的id
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 我应该避免多重实现继承吗
- 为了方便起见,我应该避免公开私有字段变量吗
- FFMPEG配置文件级别id大小无效
- 方法内部但循环仍得到预期的不合格id错误C++
- 在为LINUX创建共享库时,如何避免STL的私有/弱副本
- 避免在C++中重复子类定义
- "Inverse SFINAE"避免模棱两可的过载
- 如何避免重复具有相同产品 ID 的产品