2016-11-08 70 views
-6

我一直在嘗試練習編寫即將分配的小部分代碼,但對於我的生活我無法讓視覺工作室認識到存在if函數,它只是完全跳過它。只要按下回車鍵,窗口就會關閉。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; 

} 
+0

之間的區別很重要,不管嘗試打印a和b,請查看實際值。您的輸入中可能會發生導致意外行爲的情況。 –

+4

我可以放下你的恐懼來休息:你的'IF'陳述完全正確。當程序完成執行時,窗口就會關閉。由於程序在「IF」語句執行完成後立即執行,這就是她寫的所有內容。 –

+1

嘗試使用Ctrl + F5啓動 – Borgleader

回答

0

不可移植,但是,因爲您使用的是Visual Studio,所以您使用的是Windows,所以它可以工作。當您以調試模式工作時,使用Visual Studio,您無需將系統(「暫停」)或其他內容添加到程序中。所以我想你在Release模式下工作。

#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新線,或每次至少不是你需要一個新的生產線。你應該只寫<< "\n";std::endl插入一個新行字符+刷新緩衝區。這對了解<< std::endl;<< "\n";