0

我想让用户编辑 txt 文件并且 ItemID 是唯一字符,我想检查系统中是否存在类似 ItemID 中的用户键,会出现错误。

但是在我通过键入一个已经存在的项目 ID 进行测试后,它成功提示“库存数据库中已经存在项目 ID”但是当我尝试键入系统中不存在的另一个项目 ID 时,没有任何反应,它不会显示“返回主菜单”或退出主菜单。

请指教

    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;

休息;

4

1 回答 1

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;
}
于 2013-10-21T15:38:50.370 回答