void NewCustInfo(CustData &cust)
{
	cout << "Customer Name: ";
	getline(cin, cust.name);

}

CustData is a structure that is declared, and the name data member of that struct is of type string. For some reason when I call this function(there are no compile errors), it displays the cout statement, but then completely skips over the getline function. Any ideas?

Thanks

Dani AI

Generated

The behavior you saw (the prompt prints, then input is "skipped") is the classic case of leftover characters in the input buffer after a previous extraction. and pointed this out; below are safer, more robust ways to handle it and to avoid the problem in future code.

A concise, reliable pattern is to let getline start after any leading whitespace by using the input manipulator that eats leading whitespace, then calling getline. This keeps the read as a single operation and avoids mixing different extraction modes:

std::cout << "Customer Name: ";
std::getline(std::cin >> std::ws, cust.name);

That approach makes getline begin at the first non-whitespace character. See the manipulator reference for details: std::ws and the getline documentation: std::getline.

Another solid strategy is to consistently read whole lines and then parse them. Read each input line with getline and convert (or validate) it explicitly; this avoids surprises when mixing formatted extraction and line-based reads:

std::string line;
std::getline(std::cin, line);
int value = std::stoi(line); // validate with try/catch or checks

Quick troubleshooting notes: if input ever fails, check and clear stream error flags before retrying; validate that getline actually returned data (it can fail on EOF); and consider trimming or validating the resulting string before using it. These practices reduce hidden input bugs and make the program easier to reason about.

Recommended Answers

All 3 Replies

Does it occur after some other input? If so, there may be still be some lagging information in the input stream that it's grabbing.

Try placing cin.ignore(); directly before it.

void NewCustInfo(CustData &cust)
{
	cout << "Customer Name: ";
	getline(cin, cust.name);

}

CustData is a structure that is declared, and the name data member of that struct is of type string. For some reason when I call this function(there are no compile errors), it displays the cout statement, but then completely skips over the getline function. Any ideas?

Thanks

Its probably most likely that there is a new line character in the stream. This happens when you read in a number. As suggested,
place cin.ignore() as the first statement in the function. Then go
on normally.

Thanks guys, that fixed my problem. I shoulda been able to figure that out on my own =(

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.