Can it be done?

cout<<"ENter name"<<endl;
cout<<"Enter DOB"<<endl;
cout<<"Enter City of Birth"<<endl;

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!

Recommended Answers

All 8 Replies

Look into the std::getline function.

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

post code you tried because it does work, I've used it hundreds of times.

cin >> name >> lastname;

would work just fine too

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.

cin >> name >> lastname;

would work just fine too

Not for "Hillary Rodham Clinton"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.