问题描述
我试图在字符串变量中收集用户的输入,该变量在指定的时间内接受空格。
由于通常的 cin >> str
不接受空格,所以我会使用std :: getline从< string>
这里是我的代码: / p>
#include< iostream>
#include< vector>
#include< string>
#include< algorithm>
using namespace std;
int main()
{
int n;
cin>> n;
for(int i = 0; i {
string local;
getline(cin,local); //这根本不工作。只是跳过没有理由。
// ............................
}
//。 ...........................
return 0;任何想法? h2_lin>解决方案你可以看到为什么这是失败,如果你输出你存储在 local
(这是一个糟糕的变量名称,方式:P):
#include< iostream>
#include< vector>
#include< string>
#include< algorithm>
using namespace std;
int main()
{
int n;
cin>> n;
for(int i = 0; i {
string local;
getline(cin,local);
std :: cout<< ><<局部< std :: endl;
}
// ............................
return 0;
}
你会看到它在
后输入您的号码。
这是因为 getline
会给你留下的空行输入您的号码。 (它读取的数字,但显然不会删除 \\\
,所以你留下一个空白行。)你需要摆脱任何剩余的空格第一个:
#include< iostream>
#include< vector>
#include< string>
#include< algorithm>
using namespace std;
int main()
{
int n;
cin>> n;
cin>> ws; // stream out any whitespace
for(int i = 0; i< n; i ++)
{
string local;
getline(cin,local);
std :: cout<< ><<局部< std :: endl;
}
// ............................
return 0;
}
这样可以正常工作。
关闭主题,也许只是为手头的代码片段,但如果你没有 using namespace std; code>。它违背命名空间的目的。我怀疑这只是发布在这里,虽然。
I'm trying to collect user's input in a string variable that accepts whitespaces for a specified amount of time.
Since the usual cin >> str
doesn't accept whitespaces, so I'd go with std::getline from <string>
Here is my code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
string local;
getline(cin, local); // This simply does not work. Just skipped without a reason.
//............................
}
//............................
return 0;
}
Any idea?
解决方案 You can see why this is failing if you output what you stored in local
(which is a poor variable name, by the way :P):
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
string local;
getline(cin, local);
std::cout << "> " << local << std::endl;
}
//............................
return 0;
}
You will see it prints a newline after >
immediately after inputting your number. It then moves on to inputting the rest.
This is because getline
is giving you the empty line left over from inputting your number. (It reads the number, but apparently doesn't remove the \n
, so you're left with a blank line.) You need to get rid of any remaining whitespace first:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
cin >> ws; // stream out any whitespace
for(int i = 0; i < n; i++)
{
string local;
getline(cin, local);
std::cout << "> " << local << std::endl;
}
//............................
return 0;
}
This the works as expected.
Off topic, perhaps it was only for the snippet at hand, but code tends to be more readable if you don't have using namespace std;
. It defeats the purpose of namespaces. I suspect it was only for posting here, though.
这篇关于std :: getline在for循环中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!