【发布时间】:2021-01-12 09:47:50
【问题描述】:
cin.get() 在不传递任何参数时会做什么?
如果没有第二次 cin.get 调用(第 13 行),我的代码将无法工作:
#include <iostream>
#include <conio.h>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
char s[3][10];
for (int i=0; i <= 2; i++)
{
cin.get(s[i], 9);
cin.get();
}
cout << s[1];
getch();
}
【问题讨论】:
-
参见,例如,here。
-
对于未来的问题:'doesn't work' 通常过于不精确,无法帮助您摆脱困境。您应该向我们提供测试输入和预期输出(如果提供minimal reproducible example,我们将自己获得实际输出,所以这是可选的)。
-
假设您输入了
012345678901234567890123456789,那么您应该将012345678读入第一个子数组,然后跳过9(您没有捕获返回值)并从下一个零重新开始,所以输出将是来自第二个子数组的012345678。