如何强制 getline() 一次输入一行
How to force getline() to input one line at a time
我需要输入一定数量的字符串,每个字符串都在新行上输入。当我尝试在循环中使用 getline() 时,它会输入第一个字符串,然后立即结束。
这是我试图解决的问题,您可以在其中看到我要的输入样式:https://wcipeg.com/problem/ccc98s1
我已经查看了cin.ignore()来解决问题,但我似乎无法使其正常工作。
int n;
cin >> n;
for(int i = 0; i < n; i++) {
string input;
getline(cin, input);
cout << "Line Entered: " << input << endl;
}
如果您输入 2 表示 n,然后尝试在单独的行上输入两个字符串,则它不起作用。
int n;
cin >> n;
cin.ignore(); // are you sure you tried this?
for(int i = 0; i < n; i++) {
string input;
getline(cin, input);
cout << "Line Entered: " << input << endl;
}
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在C++中,我如何接受不同于同一行的用户输入
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- 忽略一行输入中的某些输入
- 丢失读入的每一行输入中的最后一个令牌
- 一行输入到多个变量
- 如何检查从键盘输入的特定整数值是否存在于C++文件中的一行或多行中
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 与 C++ 中一行中的用户输入数组元素相关的问题
- 如何在应用程序进行大量printf时,在固定输入的终端中设置最后一行
- 使用 getline 从输入文件读取一行到输出文件
- 标准输入未读取最后一行
- C :使用getline从文本文件中输入,要么跳过第一行,要么将其余部分弄乱
- 在一行中输入具有其他输入类型的多个单词字符串
- 如何在用户输入值后写入.csv文件的下一行,而不会覆盖以前的值?
- 如何强制 getline() 一次输入一行
- 如何让控制台在输入一行文本时输出某些内容
- 在c++中一次向字符串数组中输入一行
- 程序只在文本文件中输入一行