stream - New to C++ and wondering what getline() and cin are doing in my code -


#include <iostream> #include <string>  using namespace std;  void computefeatures( string );  int main(int argc, const char * argv[]) { string name;  cout<< "please enter full name" << endl;  cin >> name;  cout << "welcome" << name << endl;  cout << "please re-enter full name: ";  getline(cin, name);  cout << "thanks, " << name << endl;  return 0; } 

the output this:

please enter full name john smith welcomejohn please re-enter full name: thanks,  smith 

i guess question why cin print out first name , why getline() print second name. there way print both?

cin takes first word it's input , second 1 separated space waiting in input buffer. getline() gets last name automatically , doesn't wait user input.

you should use getline(cin,name) if want both first , last name in string variable name.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -