所以我有这样一个程序:
#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;
}
当我运行程序而不是一次输入一个时,我会一次性将所有输入输入终端。像这样给它输入:
4
1
2
5
3
程序输出:
进入for循环进入fac 1leaving fac离开for循环
进入for循环进入fac 2离开fac离开for循环
进入for循环进入fac 120leaving fac离开for循环
进入for循环
如您所见,它没有从标准输入接收到最后一个值。为什么会这样,谁能解释一下?
最佳答案
您可能忘记了最后一行。
如果最后不按回车键,代码还在这一行等待:
std::cin >> x;
关于c++ - stdin 没有读取输入的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43990110/