在 C++ 中使用 getline()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18786575/
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
Using getline() in C++
提问by MGE
I have a problem using getline method to get a message that user types, I'm using something like:
我在使用 getline 方法获取用户键入的消息时遇到问题,我正在使用以下内容:
string messageVar;
cout << "Type your message: ";
getline(cin, messageVar);
However, it's not stopping to get the output value, what's wrong with this?
但是,它并没有停止获取输出值,这有什么问题?
回答by Natan Streppel
If you're using getline()
after cin >> something
, you need to flush the newline character out of the buffer in between. You can do it by using cin.ignore()
.
如果您使用getline()
after cin >> something
,则需要将换行符从缓冲区中刷新出来。您可以使用cin.ignore()
.
It would be something like this:
它会是这样的:
string messageVar;
cout << "Type your message: ";
cin.ignore();
getline(cin, messageVar);
This happens because the >>
operator leaves a newline \n
character in the input buffer. This may become a problem when you do unformatted input, like getline()
, which reads input until a newline character is found. This happening, it will stop reading immediately, because of that \n
that was left hanging there in your previous operation.
发生这种情况是因为>>
运算符\n
在输入缓冲区中留下了换行符。当您进行无格式输入时,这可能会成为一个问题,例如getline()
,它会读取输入直到找到换行符。发生这种情况时,它将立即停止读取,因为\n
在您之前的操作中它被留在那里。
回答by Some programmer dude
If you only have a single newline in the input, just doing
如果输入中只有一个换行符,只需执行
std::cin.ignore();
will work fine. It reads and discards the next character from the input.
会正常工作。它从输入中读取并丢弃下一个字符。
But if you have anything else still in the input, besides the newline (for example, you read one word but the user entered two words), then you have to do
但是如果输入中除了换行符之外还有其他内容(例如,您阅读了一个单词但用户输入了两个单词),那么您必须这样做
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
See e.g. this reference of the ignore
function.
参见例如这个ignore
函数的参考。
To be even more safe, do the second alternative above in a loop until gcount
returns zero.
为了更安全,请在循环中执行上述第二个选择,直到gcount
返回零。
回答by logankilpatrick
I had similar problems. The one downside is that with cin.ignore()
, you have to press enter 1 more time, which messes with the program.
我有类似的问题。一个缺点是cin.ignore()
,您必须再按一次 Enter 键,这会使程序混乱。
回答by Antonija
int main(){
.... example with file
//input is a file
if(input.is_open()){
cin.ignore(1,'\n'); //it ignores everything after new line
cin.getline(buffer,255); // save it in buffer
input<<buffer; //save it in input(it's a file)
input.close();
}
}
回答by No Idea For Name
i think you are not pausing the program before it ended so the output you are putting after getting the inpus is not seeing on the screen right?
我认为您没有在程序结束之前暂停程序,因此您在获取输入后放置的输出没有在屏幕上看到,对吗?
do:
做:
getchar();
before the end of the program
在节目结束前
回答by Konstantin
The code is correct. The problem must lie somewhere else. Try the minimalistic example from the std::getline documentation.
代码是正确的。问题一定出在其他地方。尝试 std::getline 文档中的简约示例。
main ()
{
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Hello, " << name << "!\n";
return 0;
}