我想让用户编辑txt文件而ItemID是一个唯一的字符,我想检查系统中是否存在类似ItemID中的用户密钥,会出现错误。
但在我通过键入已存在的Item ID进行测试后,它成功提示“ItemID已存在于stock数据库中” 但是当我试图键入系统中不存在的另一个ItemID时,没有任何反应,它不会显示“返回主菜单”或退出到主菜单。
请告知
switch (choice) {
case 1:
while (true)
{
if (duplicate == true)
{
system("clear");
cout << "\E[1;32m" <<itemID << "\E[0m already exists in the stock database!" << endl;
cout << "Please enter a unique Item ID that is not used yet" << endl;
cout << "\E[1;32mItem ID:\E[0m\t" << stockVectorTemp[vectorChoice - 1].getItemID() << endl;
cout << "\E[1;32mDescription:\E[0m\t" << stockVectorTemp[vectorChoice - 1].getItemDesc() << endl;
cout << "\E[1;32mTotal Quantity:\E[0m\t" << stockVectorTemp[vectorChoice - 1].getTotalQty() << endl << endl;
}
duplicate = false;
cout << logo_EditStock << endl;
cout << "Please enter new ItemID: ";
getline(cin, input);
for (int i = 0; i < stockVector.size(); i++)
{
if (stockVector[i].getItemID() == itemID)
stockVector[i].setItemID(input);
duplicate = true;
writeStockDatabase();
// cout << "Returning to main menu..." << endl << endl;
}
if (!duplicate)
break;
}
cout << "Returning to main menu..." << endl << endl;
中断;
答案 0 :(得分:0)
你忘记了if()
陈述的开头大括号。正如评论所指出的那样,您应该改进格式:
for (int i = 0; i < stockVector.size(); i++)
{
if (stockVector[i].getItemID() == itemID)
{
stockVector[i].setItemID(input);
duplicate = true;
writeStockDatabase();
// cout << "Returning to main menu..." << endl << endl;
}
if (!duplicate)
break;
}