C++ stdin未读取输入的最后一行

C++ stdin未读取输入的最后一行,c++,C++,我有一个这样的计划: #include <iostream> using namespace std; int factorial(int x) { int result = 1; for(x; x > 1; --x) { result*=x; } return result; } int main() { // your code goes here int n; std::cin >>

我有一个这样的计划:

#include <iostream>
using namespace std;

int factorial(int x) {
    int result = 1;
    for(x; x > 1; --x) {
         result*=x;
    }
    return result;
}


int main() {
    // your code goes here
    int n;
    std::cin >> n;

    for(int i = 0; i < n; ++i) {
        std::cout<< "\n";
        std::cout << "entering for loop ";
        int x;
        std::cin >> x;
        std::cout << "entering fac ";
        std::cout << factorial(x);
        std::cout << "leaving fac ";
        std::cout << "leaving for loop ";
        std::cout << "\n";
    }
    return 0;
}
#包括
使用名称空间std;
整数阶乘(整数x){
int结果=1;
对于(x;x>1;--x){
结果*=x;
}
返回结果;
}
int main(){
//你的密码在这里
int n;
标准:cin>>n;
对于(int i=0;i你很可能忘记了最后一行新词

如果最后没有按enter键,代码仍在这一行等待:

std::cin >> x;

你确定,
阶乘
不是无限的吗loop@EdHeal--这似乎不太可能,因为它在前三次调用时都能正常工作。我无法复制。在最后的“3”之后键入了什么?factorial输出前3个值的正确值。factorial(1)、factorial(2)和factorial(5)的值输出为1、2和120,所以它似乎对这些都有效。理论上,它只是递减一个数字,直到它达到1,然后乘以一个结果值。