问题
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