CIN被跳过,Cin.ignore不起作用
Cin is being skipped and cin.ignore is not working
i/o重定向,我不能使用fstream。我通过使用"< input.txt"
运行可执行文件来使用I/O
重定向。在我的程序中,我使用while(cin>>line)
通过I/O
重定向读取文件。然后我需要cin>>x ,
,但是这次是在运行时输入的用户输入。
我已经尝试了cin.ignore, cin.clear().
如果cin
用于I/O
重定向,是否可以将cin
用于同一程序中的用户输入?
/* Not sure if this is necessary but example of input file:
x y z
a b c
j k l
*/
string line;
while(cin>>line)
{
cout<<line<<endl;
}
//I've tried these 2 lines but cin>>x is still being skipped
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
cout<<"Enter number: ";
int x;
cin>>x;// this is being skipped
我相信以下内容会起作用(未经测试(:
string line;
while(cin>>line)
{
cout<<line<<endl;
}
cin.close ();
cin.open ("/dev/tty"); // (or, on Windows, cin.open ("CON:");
...
相关文章:
- C++中的cin.ignore()函数不适用于整个流
- C++ cin.ignore() 跳过循环
- 更好的方法,而不是在C ++中使用cin.ignore
- C++ - 如何知道在哪里放置 cin.ignore
- C++:忽略第一个 cin.ignore 之后的输入
- cin.ignore() 正在产生不需要的行为
- 如何将光标移动到 cin.clear() 和 cin.ignore() 之后的文本输出末尾?
- C++ cin.ignore and getline in while loop
- CIN被跳过,Cin.ignore不起作用
- CIN.IGNORE删除第一个字符的第二次函数被调用
- cin.ignore(1)之间的分歧是什么?和cin.ignore(n)
- 使用 cin.ignore 和 cin.clear CPP 时的奇怪行为
- 为什么在"cin"后使用 "getline" 时需要 cin.ignore(),而多次使用 "cin" 时不需要?
- getline删除第一个字符;删除cin.ignore();无法解决
- C++怪异的cin.ignore行为
- Cin 不会暂停输入,尽管同时使用了 cin.clear() 和 cin.ignore()
- 当循环不中断时,使用 cin.ignore 和 cin.clear() 来验证输入
- CIN.IGNORE如何与CIN和Getline一起工作
- 尽管Cin.ignore(),CIN不等待输入
- cin.clear() 和 cin.ignore() 不起作用