How to cin whole sentence with whitespaces [closed]

泪湿孤枕 提交于 2019-12-11 08:13:58

问题


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 .


回答1:


Use cin.getline() instead:

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



回答2:


You could use std::getline(cin, tab).




回答3:


You probably want to use std::getline, specifying whatever character you want to mark the end of the sentence (e.g., '.').




回答4:


What you want is to do a getline in some form.

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



来源:https://stackoverflow.com/questions/15446951/how-to-cin-whole-sentence-with-whitespaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!