2016-04-02 66 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

超过一万亿。很简单的解决方案 –