【问题标题】:if statement with OR operator [closed]带有 OR 运算符的 if 语句 [关闭]
【发布时间】:2014-02-17 10:15:50
【问题描述】:

我正在 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;
}

【问题讨论】:

  • 为什么要这样?您的程序会问一个问题,然后另一个问题会输出一条消息或另一条消息。如果您想做某事,则必须对其进行编码。

标签: c++ if-statement operators


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 2011-01-10
    相关资源
    最近更新 更多