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!

Dani AI

Generated

The reason getline appears to be skipped is almost always a leftover newline in the input buffer from a previous formatted extraction (e.g., cin >> something). The next getline sees that newline immediately and returns an empty string. Fix it by either using getline for every field, or by consuming the pending newline before calling getline.

Example approach that works reliably:

#include <limits>
#include <string>
#include <iostream>

std::string name, dob, city;

std::cout << "Enter name\n";
std::getline(std::cin >> std::ws, name);  // eat leading whitespace/newlines

std::cout << "Enter DOB\n";
std::getline(std::cin, dob);

std::cout << "Enter City of Birth\n";
std::getline(std::cin, city);

// If you previously used operator>> somewhere:
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

If you later need separate first/last names, do not rely on operator>> since it stops at the first space. Instead, parse the full name so multi-word names are handled gracefully. One simple heuristic is to split on the last space:

auto pos = name.find_last_of(' ');
std::string first = (pos == std::string::npos) ? name : name.substr(0, pos);
std::string last  = (pos == std::string::npos) ? ""   : name.substr(pos + 1);

For details, see the references for std::getline, the whitespace manipulator std::ws, and istream::ignore.

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, networking, learning, and sharing knowledge.