C++ 如果在某些输入后使用 getline() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12691316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getline() does not work if used after some inputs
提问by Muhammad Arslan Jamshaid
Possible Duplicate:
Need help with getline()
可能重复:
需要 getline() 方面的帮助
getline()
is not working, if I use it after some inputs, i.e.
getline()
不工作,如果我在一些输入后使用它,即
#include<iostream>
using namespace std;
main()
{
string date,time;
char journal[23];
cout<<"Date:\t";
cin>>date;
cout<<"Time:\t";
cin>>time;
cout<<"Journal Entry:\t";
cin.getline(journal,23);
cout<<endl;
system("pause");
}
where as if I use getline()
on top of inputs, it does work i.e.
就像我getline()
在输入之上使用一样,它确实有效,即
cout<<"Journal Entry:\t";
cin.getline(journal,23);
cout<<"Date:\t";
cin>>date;
cout<<"Time:\t";
cin>>time;
What might be the reason?
可能是什么原因?
回答by P.P
Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
字符将被提取,直到 (n - 1) 个字符被提取或找到定界字符(如果指定了此参数,则为 delim,否则为 '\n')。如果在输入序列中到达文件末尾或在输入操作期间发生错误,提取也会停止。
When cin.getline()
reads from the input, there is a newline character left in the input stream, so it doesn't read your c-string. Use cin.ignore()
beore calling getline()
.
当cin.getline()
从输入中读取时,输入流中会留下一个换行符,因此它不会读取您的 c 字符串。使用cin.ignore()
beore 调用getline()
。
cout<<"Journal Entry:\t";
cin.ignore();
cin.getline(journal,23);
回答by jrok
Adding to what @DavidHammen said:
添加@DavidHammen 所说的内容:
The extraction operations leave the trailing '\n'
character in the stream. On the other hand, istream::getline()
discards it. So when you call getline
after an extraction operator, '\n'
is the first character it encounters and it stops reading right there.
提取操作将尾随'\n'
字符留在流中。另一方面,istream::getline()
丢弃它。因此,当您getline
在提取运算符之后调用时,'\n'
是它遇到的第一个字符,并在那里停止读取。
Put this after before getline
call extraction:
把这个放在getline
调用提取之前:
cin.ignore()
cin.ignore()
A more robust way of taking input would be something like this:
一种更强大的输入方式是这样的:
while (true) {
cout<<"Time:\t";
if (cin>>time) {
cin.ignore(); // discard the trailing '\n'
break;
} else {
// ignore everything or to the first '\n', whichever comes first
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.clear(); // clear the error flags
cout << "Invalid input, try again.\n";
}
}
回答by David Hammen
You're not checking stream status. The std::cin
stream extraction operator (operator>>
) can fail. When it does, the stream is marked as "bad" (failbit
, badbit
, or eofbit
are set). Once "bad", all subsequent stream extractions on that stream will fail unless you clear the status.
您没有检查流状态。该std::cin
流提取运算符(operator>>
)可能会失败。当它,流被标记为“坏”( ,failbit
,badbit
或eofbit
设置)。一旦“坏”,除非您清除状态,否则对该流的所有后续流提取都将失败。
Learn to be a paranoid programmer. Always check status of those formatted input operations. You could, for example throw an exception, or print an error message and exit. The one thing you shouldn't do is to simply assume that it worked.
学习成为一个偏执的程序员。始终检查那些格式化输入操作的状态。例如,您可以抛出异常,或打印错误消息并退出。你不应该做的一件事就是简单地假设它有效。