C++ 如何用空格将整个句子 cin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15446951/
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
How to cin whole sentence with whitespaces
提问by Yoda
I have tried:
我试过了:
char tab[200];
cin>>tab;
cout<<tab<<endl;
and I would like to make that even if I input in console A B C
all the 3 chars and spaces go into tab
at once .
即使我在控制台中输入A B C
所有 3 个字符和空格,我也想做到这一点tab
。
回答by Tuxdude
回答by NPE
You could use std::getline(cin, tab)
.
你可以使用std::getline(cin, tab)
.
回答by Jerry Coffin
You probably want to use std::getline
, specifying whatever character you want to mark the end of the sentence (e.g., '.').
您可能想要使用std::getline
, 指定要标记句子结尾的任何字符(例如,'.')。
回答by dutt
What you want is to do a getline in some form.
你想要的是以某种形式做一个 getline 。
string str;
cin.get(str, 25);
cout <<"\"" <<str <<"\"" <<endl;
or
或者
string str;
cin.getline(str, 25);
cout <<"\"" <<str <<"\"" <<endl;
Both accept a third parameter, a char, that specifies what to consider the end of the line. For details see http://www.minich.com/education/wyo/cplusplus/cplusplusch10/getfunction.htm
两者都接受第三个参数,一个字符,它指定要考虑行尾的内容。详情见http://www.minich.com/education/wyo/cplusplus/cplusplusch10/getfunction.htm