C ++嵌套循环问题

丹尼

好的,这很奇怪……我正在为该课程做最后的项目,它有一系列的循环,这些循环告诉程序根据用户输入做什么。他们工作得很好。然后,我在其中一个循环中编写了另一个循环,程序突然无法正常工作-意味着我只是对它进行了简单的测试,却失败了。我检查了一下,并且编写的代码与其他任何循环都没有不同,所以我认为这不是问题。我也尝试过切换编译器,但也没有改变。

这是循环结构的问题代码:

vector<Item*> inventory;
string usrInptOptn = "default";
string usrInptOptn2 = "default";

while (true) {
    // Get user choice
    cout << "\nEnter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
    getline(cin, usrInptOptn);

    // Process user choice
    if (usrInptOptn.size() == 0) {
        continue;
    }
    else if (usrInptOptn.at(0) == 'p') {
        PrintInventory(inventory);
    }
    else if (usrInptOptn.at(0) == 'a') {
        cout << "\nEnter (b)ook or (p)roduce: ";
        getline(cin, usrInptOptn);

        if (usrInptOptn2.at(0) == 'b') {
            cout << "needs work..." << endl;            //Something fishy going on here...
        }
        else if (usrInptOptn2.at(0) == 'p') {
            cout << "something isn't working here" << endl;
            inventory = AddItemToInventory(inventory);
        }
    }
    else if (usrInptOptn.at(0) == 'u') {
        inventory = UpdateItemQtyInInventory(inventory);
    }
    else if (usrInptOptn.at(0) == 'r') {
        inventory = RemoveItemFromInventory(inventory);
    }
    else if (usrInptOptn.at(0) == 'q') {
        cout << "\nGood bye." << endl;
        break;
    }
}

任何帮助将不胜感激!:D预先感谢!-丹尼

只是好奇

你做了

getline(cin, usrInptOptn);

代替

getline(cin, usrInptOptn2);

就目前而言,无效的部分正在检查“默认”一词

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章