# [C++] cin::ignore and cin::clear not working.

## Recommended Posts

My program uses the following snip.
cin.getline(line,30);
if ( !cin )
{
cin.ignore(1000); // Also tried with new line
cin.clear(); // Placed before ignore() as well
}

The if is entered fine, and both methods are called. However, a later Y/N confirmation is "answered for me" later in the program, making the query impossible to address manually. cin is still not completely cleared out, so what am I missing?

##### Share on other sites
That looks fine to me. Are you sure there isn't any other input happening between the clearing and your Y/N input? Can we see *all* of the input code and what you actually want to input?

Other than that, I'd replace the 1000 with std::cin.rdbuf()->in_avail() (how many characters are actually in the buffer).

##### Share on other sites
Well, I later use this.

template<typename T>T& SafeCIN(const std::string& msg){	static T buf;	// Output query	cout << msg << ": ";	// While the stream is invalid...	while (!(cin >> buf))	{		// Inform user of erroneous input.		cout << "Invalid Input.\n" << endl;		// Clean up cin.		cin.ignore(1000);		cin.clear();		// Output query again.		cout << msg << ": ";	}	endl(cout);	return buf;}

This is the only other time I manage input at all.

char choice = SafeCIN<char>("Would you like to see the source code (Y/N)?");

##### Share on other sites
Hm.. something weird is going on :/

Try calling clear() before ignore(), any changes?

##### Share on other sites
Making use of std::cin.rdbuf()->in_avail() and then placing clear() before ignore() did the trick. I'm not sure what EXACTLY I changed between the two code bodies, as I've been trying to provoke the error again by shifting the order around. It didn't come back, so I'm guessing a part of my program hit undefined behavior.

Thanks for the help!

##### Share on other sites
Quote:
 Original post by zyrolastingMaking use of std::cin.rdbuf()->in_avail() and then placing clear() before ignore() did the trick. I'm not sure what EXACTLY I changed between the two code bodies, as I've been trying to provoke the error again by shifting the order around. It didn't come back, so I'm guessing a part of my program hit undefined behavior.

Good to hear [smile].
Quote:
 Thanks for the help!

No prob; I also hate these kinds of subtle stream problems [grin].

The most annoying one has to be this:
float n;std::cin >> n; // I input something like "3.14" and hit enterstd::string line;// the free-function std::getline() works with std::stringstd::getline(std::cin, line); // I want to input something like "hello world"

Now line will be empty instead of containing "hello world". Can you guess what happened? It turns out that the first input read the number, but only up to the '\n' and left the '\n' there since it's not a number. Then the getline() after that read everything up to the '\n' (that is, nothing) and then chewed up the '\n', "successfully" reading the line without even asking for the "hello world" part [grin]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628389
• Total Posts
2982410

• 10
• 9
• 19
• 24
• 11