if语句与OR运算符

时间:2014-01-26 16:41:41

标签: c++ if-statement operators

我在Visual Studio 2013上编写此代码。

当我通过回答(1)第一个问题执行代码时,程序仍然问我第二个问题。

如果我回答(1)第一个问题,程序是否应该跳过第二个问题?

#include <iostream>
using namespace std;

int main()
{
    cout << "Answer questions with 0 or 1" << endl;
    cout << "Is there a deep discount on your favorite car? ";
    bool Discount = false;
    cin >> Discount;

    cout << "Did you get a fantastic bonus? ";
    bool FantasticBonus = false;
    cin >> FantasticBonus;

    if (Discount || FantasticBonus)
        cout << "Congratulations, you can buy that car!" << endl;
    else
    cout << "Sorry, waiting a while is a good idea" << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:2)

由于在第一个答案之后第二个问题之前没有任何条件,因此不会跳过第二个问题。如果您想在第一个答案输入1时跳过第二个问题:

bool FantasticBonus = false;
if(!Discount) {
    cout << "Did you get a fantastic bonus? ";
    cin >> FantasticBonus;
}