带空格的 C++ 输入字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40984752/
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
C++ Input String with Spaces
提问by stefan.kenyon
I am writing a program that takes input from the user. I need the input to include spaces between the words. I am having trouble finding a solution to do that. Before you ask, I have tried multiple other questions on stackoverflow with the same question. These are some of the ones I have tried. How to cin Space in c++?
我正在编写一个接受用户输入的程序。我需要输入在单词之间包含空格。我很难找到解决方案来做到这一点。在你问之前,我已经尝试过关于 stackoverflow 的多个其他问题,同样的问题。这些是我尝试过的一些。 如何在 C++ 中创建空间?
Demonstration of noskipws in C++
The problem with my code is that as soon as my setBusinessName method is called, it just completes itself. It outputs and then returns itself without waiting for me to input my data. Help Required...
我的代码的问题是,只要我的 setBusinessName 方法被调用,它就会自行完成。它输出然后返回自己而不等待我输入我的数据。需要帮助...
string setBusinessName()
{
string name = "";
cout << "The name you desire for your business:";
getline(cin, name, '\n');
cout << name;
return name;
}
回答by Hyman Of Blades
I can't comment yet, don't have enough points, but did you try adding cin.ignore();
before the getline(cin, name, '\n');
?
我还不能发表评论,没有足够的积分,但是您是否尝试cin.ignore();
在getline(cin, name, '\n');
?之前添加?
Like this:
像这样:
string setBusinessName()
{
string name = "";
cout << "The name you desire for your business:";
cin.ignore();
getline(cin, name, '\n');
cout << name;
return name;
}
回答by M.M
Just adding some more explanation to the comments, when you do:
只需在评论中添加更多解释,当您这样做时:
cout << "Enter value:";
cin >> x;
The cin instruction is executed when the user presses Enter, so the input buffer has the value the user inserted and an extra '\n'
char. If you continue doing cin
that is ok, but if you want to use getline
(like in your case to include spaces in a string) you must be aware that getline
will stop at the first occurence of '\n'
in the buffer, so the result from getline
will be empty.
cin 指令在用户按下时执行Enter,因此输入缓冲区具有用户插入的值和一个额外的'\n'
字符。如果您继续这样做cin
是可以的,但是如果您想使用getline
(例如在您的情况下在字符串中包含空格),您必须知道getline
它将'\n'
在缓冲区中第一次出现时停止,因此结果 fromgetline
将为空。
To avoid this, and if you really must use both cin and getline, you need to remove that '\n'
from the buffer by using cin.ignore(streamsize n = 1, int delim = EOF), this function clears streamsize
chars from the buffer oruntil the first char that matches delim
(including), here's an example:
为避免这种情况,如果您确实必须同时使用 cin 和 getline,则需要'\n'
使用cin.ignore(streamsize n = 1, int delim = EOF)将其从缓冲区中删除,此函数streamsize
从缓冲区中清除字符或直到匹配delim
(包括)的第一个字符,这是一个示例:
cin << x;
cin.ignore(256, '\n');
getline(cin, name, '\n');
Noteit is advisable to use:
请注意,建议使用:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if you don't want to guess how many chars are in the buffer.
如果您不想猜测缓冲区中有多少个字符。
回答by theelee
It's possible that there is already something in the stream and getline()
just reads it.
流中可能已经存在某些getline()
内容并且只是读取它。
Make sure you didn't use cin>>
before this function.
And you can use cin.ignore()
before getline()
to avoid something already existed in the stream.
确保您cin>>
在此功能之前没有使用过。你可以使用cin.ignore()
beforegetline()
来避免流中已经存在的东西。
回答by coderpc
It is working fine. I just tried this.
它工作正常。我刚试过这个。
#include <iostream>
#include <string>
using namespace std;
string setBusinessName(){
string name = "";
cout << "The name you desire for your business:";
getline(cin, name);
cout << name;
return name;
}
int main() {
setBusinessName();
system("PAUSE");
}