help pleaseee getline() is being skipped

closed account (yh54izwU)
I am working on a little program and for some reason when I try and use the getline() it is giving me a lot of problems here is what I did


getline(cin, s.name); //it skips this altogether
cout<<"Test "<<s.name; //here it will display test but nothing else
getline(cin, s.address);
getline(cin, s.city);
getline(cin, s.state);
cin>>s.zip;

is there anything I am doing wrong or need to do extra to it (and yes I #include <iostream> I even did <iomanip> just to see if that would help)?
Last edited on
I don't quite understand what he should do in this situation, even with the link you provided cire. I understand it is frustrating to you when the "obvious" answer is ALREADY answered somewhere... but I still don't get it.
OK, I see that "http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6" says, "Because the numerical extractor leaves non-digits behind in the input buffer," but their recommendation to fix the issue sucks.

Doesn't work, I mean. Throws errors.

My code:
#include <iostream>
using namespace std;

int main()
{
cout << "Please enter two words: " << endl;

string b, c;
cin >> b >> c;

cout << "I understood: "
<< b << ", and "
<< c << endl;

cout << "Now, type in a whole line of text, "
<< "with as many blanks as you want.:"
<< endl;

string wholeLine;
getline(cin, wholeLine);

cout << "I understood: \"" << wholeLine << "\"" << endl;

return 0;

When I run the program it just skips right over. If I add the solution from that site, it just errors out.
http://answers.yahoo.com/question/index?qid=20091206224451AAkAIji

It has something to do with the pressing enter. cin.ignore() is useful here.
You have a different problem than the one described in the faq. You are mixing formatted and unformatted input. formatted input leaves trailing newlines in the stream. Your unformatted input, getline, stops when it encounters a newline.

Try the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;

int main()
{
	cout << "Please enter two words: " << endl;

	string b, c;
	cin >> b >> c;

	cout << "I understood: "
		<< b << ", and "
		<< c << endl;

	cout << "Now, type in a whole line of text, "
		<< "with as many blanks as you want.:"
		<< endl;

	if ( cin.peek() == '\n' )
		cin.ignore();

	string wholeLine;
	getline(cin, wholeLine);

	cout << "I understood: \"" << wholeLine << "\"" << endl;

	return 0;
}
Topic archived. No new replies allowed.