#include <iostream>
#include <string.h>
#include<stdio.h>
using namespace std;
int main()
{
char d,a[9],e[9];
cin.getline(a,9);
cin.getline(e,9);
cin>>d;
puts(a);
puts(e);
cout<<d<<endl;
return 0}
当我在输出屏幕上输入“大家好”时,puts(e)
打印一个空行,d 打印一个随机字符。second getline
功能和cin
由于第一个 getline
,功能无法正常工作.提前致谢。
最佳答案
你应该读一些documentation对于你使用的东西:
After constructing and checking the sentry object, extracts characters from *this and stores them in successive locations of the array whose first element is pointed to by s, until any of the following occurs (tested in the order shown):
- [...]
- count-1 characters have been extracted (in which case setstate(failbit) is executed).
(强调我的)
因此,如果输入太长,第一个 getline
将失败,从而使 std::cin
处于不良(即不可读)状态。这使得所有连续的输入操作立即失败。
备注:为了避免大量丑陋的麻烦,您应该避免使用 C 风格的字符串和“某种 C 风格”成员-getline
,而只使用 std::string
和非成员(member)std::getline
相反。
关于c++ - 当我超过第一个 getline() 的输入数组大小时,第二个 getline 或其他输入函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295034/