cin.ignore() 正在产生不需要的行为
cin.ignore() is producing undesired behavior
对于以下代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
string s;
getline(cin,s);
cout << s << "n";
cin.ignore(1000,'n');
}
return 0;
}
示例输入:
阿拉伯数字
名称1
名称2
预期产出:
名称1
名称2
错误输出:
Name2//后跟空行
我不知道为什么会这样。我尝试了堆栈溢出上给出的所有解决方案,但不幸的是没有一个对我有用。提前谢谢。
cin.ignore()
用于在输入整数后忽略新行。 因此,在cin>>t
后使用cin.ignore()
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
cin.ignore();
while(t--)
{
string s;
getline(cin,s);
cout << s << "n";
}
return 0;
}
相关文章:
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 为什么output_editor Concept不需要output_e迭代器标记
- 在除法中不需要四舍五入
- 与C代码相比,为什么C++代码不需要"#define _POSIX_C_SOURCE 200809L"?
- 将值从另一个数组写入数组,不包括不需要的值 C++
- C++ 写入路径名中包含不需要的空字符的文件
- 是否有必要获取锁并在不需要唤醒线程时通知condition_variable?
- 使用 assimp 加载模型 - 不需要提升?
- 为什么转换函数声明不需要至少一个定义类型说明符
- 返回不需要的值的二叉搜索程序
- 当我不需要数据库中的所有值时,如何部分初始化 c++ 对象?
- C++:用户输入会产生不需要的行为
- 如何在不需要LIBCD.lib的情况下在Visual Studio 6中编译C项目
- 为什么python需要全局关键字而C/C++不需要?
- 将一个宏传递到另一个宏而不是直接传递内容时会出现不需要的额外"空"参数
- 为什么我们需要在 C++ 中检查空指针,而在 Java 中不需要?
- 为什么复制构造函数不需要检查输入对象是否指向自身?
- cin.ignore() 正在产生不需要的行为
- 为什么在"cin"后使用 "getline" 时需要 cin.ignore(),而多次使用 "cin" 时不需要?