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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:22:19  来源:igfitidea点击:

How to cin whole sentence with whitespaces

c++charcin

提问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 Call the 3 chars and spaces go into tabat once .

即使我在控制台中输入A B C所有 3 个字符和空格,我也想做到这一点tab

回答by Tuxdude

Use cin.getline()instead:

使用cin.getline()来代替:

char tab[200];
cin.getline(input,200);
cout<<tab<<endl;

回答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