std :: cin输入带有空格?
问题描述:
#include <string>
std::string input;
std::cin >> input;
用户想要输入 Hello World。但是 cin
在两个单词之间的空格处失败。如何使 cin
进入整个
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
?
我实际上是在使用结构体执行此操作,而 cin.getline
似乎不起作用。这是我的代码:
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?
答
您必须使用 cin.getline()
:
You have to use cin.getline()
:
char input[100];
cin.getline(input,sizeof(input));