【问题标题】:Visual Studio Appears to be ignoring my IF functionVisual Studio 似乎忽略了我的 IF 函数
【发布时间】:2017-03-21 12:44:50
【问题描述】:

我一直在尝试为即将完成的任务练习我的一小部分编码,但在我的一生中,我无法让 Visual Studio 认识到有一个 if 函数,它只是完全跳过它。按下回车后窗口立即关闭。

#include <iostream>
using namespace std;

int main()
{
    int a, b;
    cout << "Enter first number.";
    cin >> a;

    cout << "Enter second number.";
    cin >> b;
    if( a > b)

    {
        cout << "Variable a is greater than variable b." << endl;
        cout << "Value of a is " << a << " value of b is " << b << endl;

    }

    return 0;

}

【问题讨论】:

  • 无论如何都尝试打印 a 和 b,看看实际值是多少。您的输入中可能发生了一些导致意外行为的情况。
  • 我可以消除您的恐惧:您的IF 声明非常好。当程序完成执行时,窗口就会关闭。由于程序在IF语句执行完成后立即执行完毕,所以她写的就是这些。
  • 尝试使用 Ctrl+F5 启动

标签: c++ visual-studio-2010


【解决方案1】:

不可移植,但由于您使用的是 Visual Studio,因此您使用的是 Windows,所以它可以工作。当您在调试模式下工作时,使用 Visual Studio,您不需要将 system("pause") 或其他内容添加到您的程序中。所以我猜你是在发布模式下工作。

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    int a, b;
    cout << "Enter first number.";
    cin >> a;

    cout << "Enter second number.";
    cin >> b;
    if( a > b)

    {
        cout << "Variable a is greater than variable b." << endl;
        cout << "Value of a is " << a << " value of b is " << b << endl;

    }

    system("pause");
    return 0;

}

编辑: 您不应该将std::endl 用于新行,或者至少不要在每次需要新行时使用。您应该只写 &lt;&lt; "\n"; std::endl 插入一个新行字符 + 刷新缓冲区。了解&lt;&lt; std::endl;&lt;&lt; "\n"; 之间的区别很重要

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2016-09-30
    相关资源
    最近更新 更多