problem with successive getline() calls and entering EOF on terminal

Please look at the following code, I believe the comments make the problem fairly obvious to understand ..

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
30
31
#include <iostream>
using namespace std;


// function prototypes
void print_state(istream& in);

int main()
{
	string x,y;
	getline(cin, x); 	// when user enters EOF (CTRL + D) here

	cin.clear();

	print_state(cin); 	// state of istream object appears to be good.
	getline(cin,y); 	// does not wait for user to enter input. Why?
	print_state(cin);


	cout << "x: " << x << endl;
	cout << "y: " << y << endl;
}

void print_state(istream& in)
{
	cout << endl << "\tgood: " << in.good() << endl;
	cout << "\tbad: " << in.bad() << endl;
	cout << "\teof: " << in.eof() << endl;
	cout << "\tfail: " << in.fail() << endl;
}


The output from the terminal:


^D
	good: 1
	bad: 0
	eof: 0
	fail: 0

	good: 0
	bad: 0
	eof: 1
	fail: 1
x: 
y:
User $ 


UPDATE 1: (Thanks to jlb for the help ..)
The code works fine in Linux. (I tested it on Ubuntu, gcc version 4.6.3)
But shows the above behaviour on MAC OSX, gcc version 4.2.1 ... Hope some one can help me solve this ..

UPDATE 2: Solution!! (Thanks to Cubbi ... )
adding the following after line 13 to clear the error bits of the stdin stream solves the problem.
clearerr(stdin);

Note:
I need the user to Enter EOF (to detect end of input).
Because I intend to read and store a block of text (spanning multiple lines) from the user like so:
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
istream& get_textBlock(istream& in, vector<string>& vec)
{
	vector<string>::size_type s1 = vec.size(), s2;
	string x;

	cout << "\tEntered get_textBlock() ... \n";
	print_state(in);

	if(in)
	{
		while(getline(in, x))
		{
			vec.push_back(x);
		}

		in.clear();

		s2=vec.size();
		if(s1==s2)
		 	throw domain_error ("get_textBlock: No Text Entered");

	}
	else
	{
		throw domain_error("istream in error state ... \n");
	}

	return in;
}


PS: if something is not clear, please let me know. I'll try explaining further. Thank you!
Last edited on
When I run the program after entering end of file the program waits for input; When I enter any input it continues to the end of the program.

Hey, Thanks for the reply,

Just in case .. I tried the same code again( I'm using MAC OSX, G++ compiler) but it behaves as I mentioned earlier .. it's not waiting at the getline on [line 16]

Could you tell me what OS you are on and the compiler you used .. though i'm not sure it depends on the OS..



Last edited on
I used g++ version 4.7.2 on Linux. Did you run the program in a command window, or thru an IDE? If an IDE what IDE are you using?

not an ide .. the command window (aka Terminal) .. n gcc version 4.2.1 ...

Also I just tested it on ubuntu (parallels), gcc 4.6.3 and it works !
but hope someone can help with the issue on Mac ...
Maybe you must clear the input buffer with cin.ignore() function before the next input to get rid of any unwanted newline characters.

HTH,
Aceix.
once the input is closed, there is no portable way to reopen it. Imagine how this would work if the program is running with input redirection, reading from a file.
that said, try clearerr(stdin); in addition to your cin.clear(); -- C streams have their own error bits.
@cubbi
clearerr() does the job. Thanks a lot.

Also, could you point me to some good literature where I can learn more about streams (C and C++) and the related operations.
@Aceix

Thanks for the reply, but I don't think unwanted characters are causing the issue here. The stream is already empty, I am only passing EOF as the input at the first getline() call on line 11, and using getline() function removes the newline character from the stream too .. (although after EOF I dont think the newline matters)

PS: Cubbi's suggestion solved my problem. You might want to look at it. :)
Last edited on
Topic archived. No new replies allowed.