STD :: CIN输入带空间?
#include <string>
std::string input;
std::cin >> input;
用户想输入“ Hello World”。但是cin
在两个单词之间的空间上失败。如何使cin
纳入整个Hello World
?
我实际上是使用structs和cin.getline
做到这一点的。这是我的代码:
struct cd
{
std::string CDTitle[50];
std::string Artist[50];
int number_of_songs[50];
};
std::cin.getline(library.number_of_songs[libNumber], 250);
这会产生错误。有什么想法吗?
#include <string>
std::string input;
std::cin >> input;
The user wants to enter "Hello World". But cin
fails at the space between the two words. How can I make cin
take in the whole of Hello World
?
I'm actually doing this with structs and cin.getline
doesn't seem to work. Here's my code:
struct cd
{
std::string CDTitle[50];
std::string Artist[50];
int number_of_songs[50];
};
std::cin.getline(library.number_of_songs[libNumber], 250);
This yields an error. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用:
该功能可以在
Use :
the function can be found in
您想在CIN中使用.getLine函数。
从 there 。检查一下,以获取更多信息和示例。
You want to use the .getline function in cin.
Took the example from here. Check it out for more info and examples.
如何从输入中读取字符串?
您可以读取一个带有
std :: cin
的单个空格终止的单词,例如:请注意,没有明确的内存管理,没有您可能会溢出的固定尺寸缓冲区。
如果您确实需要一条线(而不仅仅是一个单词),则可以做到这一点:
How do I read a string from input?
You can read a single, whitespace terminated word with
std::cin
like this:Note that there is no explicit memory management and no fixed-sized buffer that you could possibly overflow.
If you really need a whole line (and not just a single word) you can do this:
C方式
您可以使用
get
在cstdio(c)中找到的函数:c ++方式
get
在C ++ 11中删除。[推荐]:您可以使用 getline(cin,name)
String.h
或 cin.getline(name,256)
iostream中
本身。THE C WAY
You can use
gets
function found in cstdio(stdio.h in c):THE C++ WAY
gets
is removed in c++11.[Recommended]:You can use getline(cin,name) which is in
string.h
or cin.getline(name,256) which is in
iostream
itself.我宁愿使用以下方法获取输入:
它实际上非常易于使用,而不是定义包含空格字符的字符串的总长度。
I rather use the following method to get the input:
It's actually super easy to use rather than defining the total length of array for a string which contains a space character.
它不会“失败”;它只是停止阅读。它将词汇令牌视为“字符串”。
使用
std :: getline
getline :请注意,请注意这与不是与
std :: istream :: getline
相同,它与C-stylechar
char buffer而不是std: :String
s。更新
您的编辑问题与原件几乎没有相似之处。
您正在尝试将
getline
纳入int
,而不是字符串或字符缓冲区。流的格式操作仅适用于运算符&lt;
和操作员&gt;&gt;
。要么使用其中一个(并因此使用getline
,然后词法转换为int
,要么使用getline
,要么使用其中一个。It doesn't "fail"; it just stops reading. It sees a lexical token as a "string".
Use
std::getline
:Note that this is not the same as
std::istream::getline
, which works with C-stylechar
buffers rather thanstd::string
s.Update
Your edited question bears little resemblance to the original.
You were trying to
getline
into anint
, not a string or character buffer. The formatting operations of streams only work withoperator<<
andoperator>>
. Either use one of them (and tweak accordingly for multi-word input), or usegetline
and lexically convert toint
after-the-fact.您必须使用
cin.getline()
::You have to use
cin.getline()
:标准库提供了一个称为
ws
的输入函数,该功能从输入流中消耗了空格。您可以这样使用:The Standard Library provides an input function called
ws
, which consumes whitespace from an input stream. You can use it like this: