程序无法正常运行

时间:2015-07-10 18:01:17

标签: c++

我正在制作一个关于订购汉堡和苏打水的有趣计划。我没有经过测试就完成了所有工作,并且无法正常工作。

代码:

#include <iostream>
using namespace std;

int orderDrinks();
int orderBurgers();

int main()
{
    orderBurgers();
    orderDrinks();
    return 0;
}
int orderBurgers()
{
    bool(orderBurgersLoop);
    while (orderBurgersLoop = true)
    {
        cout << "How many burgers would you like to order?" << endl;
        int burgersAmount;
        cin >> burgersAmount;

        bool(burgersAmountLoop);
        while (burgersAmountLoop = true)
        {
            if (burgersAmount > 1)
                cout << "You ordered " << burgersAmount << " burgers. We will notice you when they are done." << endl;
            else if (burgersAmount = 1)
                cout << "You ordered " << burgersAmount << " burger. We will notice you when it's done!" << endl;
            else if (burgersAmount <= 0)
                cout << "Are you sure?" << endl;
            char orderBurgersLeaveOrStay;
            cin >> orderBurgersLeaveOrStay;
            bool(orderBurgersLeaveOrStayLoop);
            while (orderBurgersLeaveOrStayLoop = true)
            {
                if (orderBurgersLeaveOrStay = 'Yes')
                    orderBurgersLoop = false;
                if (orderBurgersLeaveOrStay = 'No')
                {
                    orderBurgersLeaveOrStayLoop = false;
                    burgersAmountLoop = false;
                    orderBurgersLoop = false;
                }
            }
        }
    }
    return 0;
}
int orderDrinks()
{
    cout << "Would you like to order anything to drink?" << endl;
    char orderDrinksOption;
    cin >> orderDrinksOption;

    bool(orderDrinksLoop);
    while (orderDrinksLoop = true)
    {
        if (orderDrinksOption = 'Yes')
        {
            cout << "How many drinks would you like to order?" << endl;
            int drinksAmount;
            cin >> drinksAmount;
            if (drinksAmount = 1)
                cout << "You ordered " << drinksAmount << " drinks. We will notice you when they are done!" << endl;
            else if (drinksAmount > 1)
                cout << "You ordered " << drinksAmount << " drink. We will notice you when it's done!" << endl;
        }
        if (orderDrinksOption = 'No')
            orderDrinksLoop = false;
        else
            cout << "Yes or No?";
    }
    return 0;
}

在第一个问题上,如果我输入0,它会打印回来“你订购了1个汉堡,blabla”,它就像我想要的那样无法正常工作。我在做什么(写作)错了?

1 个答案:

答案 0 :(得分:1)

您的所有比较都使用错误的运算符

burgersAmountLoop = true

要比较您使用==

burgersAmountLoop == true

前者是一个赋值,它返回赋值的值,因此你有效地进行了无限循环。

无论如何,考虑到变量是bool,进行任何比较都是多余的

while (burgersAmountLoop)