2016-04-02 80 views
-1

我從教科書中運行此代碼:介紹使用QT設計C++模式。C++代碼導致可能的控制檯輸出錯誤

/* Computes and prints n! for a given n. 
Uses several basic elements of C++. */ 

#include <iostream> 
int main() { 

    using namespace std; 
    /* 
     */ 

    // Declarations of variables 
    int factArg = 0; 
    int fact(1); 
    do { 
     cout << "Factorial of: "; 
     cin >> factArg; 
     if (factArg < 0) { 
      cout << "No negative values, please!" << endl; 
     } 
    } 
    while (factArg < 0); 
    int i = 2; 

    while (i <= factArg) { 
     fact = fact * i; 
     i = i + 1; 
    } 
    cout << "The Factorial of " << factArg << " is: " << fact << endl; 
    return 0; 
} 

輸出控制檯只打印一行寫着「階乘是:」 難道這就是它想幹什麼?

enter image description here

+0

它在我身邊工作。你確定控制檯允許執行輸入嗎? – vsoftco

回答

0

是的,這是什麼,程序要在第一輸出;它正在等待你輸入一個數字。如果你進入你的代碼,你會很快發現它會等待下面一行的輸入:「cin >> factArg;」。

所以...繼續,輸入一個數字,然後按回車:)。

-1

是的,您的代碼包括cin >> factArg,您將在程序運行之前首先在終端中輸入。您可能希望將using namespace std放在主函數之前,而不是放在其中。

+0

超過一萬億。很簡單的解決方案 –