C++ std::cin 输入有空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5838711/
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
std::cin input with spaces?
提问by lemon
#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
?
用户想要输入“Hello World”。但是cin
在两个单词之间的空格处失败。我怎样才能让cin
整个Hello World
?
I'm actually doing this with structs and cin.getline
doesn't seem to work. Here's my code:
我实际上是用结构来做这件事的,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);
This yields an error. Any ideas?
这会产生错误。有任何想法吗?
采纳答案by Pete
回答by Lightness Races in Orbit
It doesn't "fail"; it just stops reading. It sees a lexical token as a "string".
它不会“失败”;它只是停止阅读。它将词法标记视为“字符串”。
Use std::getline
:
使用std::getline
:
int main()
{
std::string name, title;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Enter your favourite movie: ";
std::getline(std::cin, title);
std::cout << name << "'s favourite movie is " << title;
}
Note that this is notthe same as std::istream::getline
, which works with C-style char
buffers rather than std::string
s.
请注意,这是不一样std::istream::getline
,这与C风格的作品char
缓冲区,而不是std::string
秒。
Update
更新
Your edited question bears little resemblance to the original.
您编辑的问题与原始问题几乎没有相似之处。
You were trying to getline
into an int
, not a string or character buffer. The formatting operations of streams only work with operator<<
and operator>>
. Either use one of them (and tweak accordingly for multi-word input), or use getline
and lexically convert to int
after-the-fact.
您试图getline
进入一个int
,而不是字符串或字符缓冲区。流的格式化操作仅适用于operator<<
和operator>>
。要么使用其中之一(并针对多词输入进行相应的调整),要么使用getline
并在词法上转换为int
事后。
回答by dev gr
The Standard Library provides an input function called ws
, which consumes whitespace from an input stream. You can use it like this:
标准库提供了一个名为 的输入函数ws
,它使用输入流中的空格。你可以这样使用它:
std::string s;
std::getline(std::cin >> std::ws, s);
回答by Gautham Vinod
Use :
用 :
getline(cin, input);
the function can be found in
该功能可以在
#include <string>
回答by Cody
You want to use the .getline function in cin.
您想在 cin 中使用 .getline 函数。
#include <iostream>
using namespace std;
int main () {
char name[256], title[256];
cout << "Enter your name: ";
cin.getline (name,256);
cout << "Enter your favourite movie: ";
cin.getline (title,256);
cout << name << "'s favourite movie is " << title;
return 0;
}
Took the example from here. Check it out for more info and examples.
以这里为例。查看它以获取更多信息和示例。
回答by abe312
THE C WAY
C方式
You can use gets
function found in cstdio(stdio.h in c):
您可以使用gets
cstdio(stdio.h in c) 中的函数:
#include<cstdio>
int main(){
char name[256];
gets(name); // for input
puts(name);// for printing
}
THE C++ WAY
C++ 方式
gets
is removed in c++11.
gets
在 c++11 中被删除。
[Recommended]:You can use getline(cin,name)which is in string.h
or cin.getline(name,256)which is in iostream
itself.
[推荐]:您可以使用函数getline(CIN,名),这是在string.h
或cin.getline(名称,256),这是在iostream
本身。
#include<iostream>
#include<string>
using namespace std;
int main(){
char name1[256];
string name2;
cin.getline(name1,256); // for input
getline(cin,name2); // for input
cout<<name1<<"\n"<<name2;// for printing
}
回答by Rohan Bari
I rather use the following method to get the input:
我宁愿使用以下方法来获取输入:
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string name;
cout << "Hello, Input your name please: ";
getline(cin, name);
return 0;
}
It's actually super easy to use rather than defining the total length of array for a string which contains a space character.
它实际上非常易于使用,而不是为包含空格字符的字符串定义数组的总长度。