Can it be done?

cout<<"ENter name"<<endl;
cin>>name;
cout<<"Enter DOB"<<endl;
cin>>dob;
cout<<"Enter City of Birth"<<endl;
cin>>city;

when i try to enter "John Doe" as name the program skips and doesn't let me enter anything for dob and asks me the city input.

Any help would be greatly appreciated. Thanks!

The >> operator stops at the first space. To get everything you need to use getlin(). There are two versions: one for std::string and the other for character arrays.

cout<<"ENter name"<<endl;
getline(cin, name); // assum name is std::string

cin.getline(name, sizeof(name)); // assumes name is a character array

thanks to both of you for your fast response...i tried to use the getline function before (getline(cin, name);) but the same thing is happening. I don't know why. It doesn't even let me put anything for the name. It forwards me to the next cout statement

Its better to use getline(cin, variablename) unless you want to use the first and last name seperately.

getline() approach also allows handling of cases where someone enters a partial name (eg first name only vs first and last name). However, it is necessary to parse the line input to determine whether it contains one or two (or more) fields.

This article has been dead for over six months. Start a new discussion instead.