Sign in to follow this  
zyrolasting

[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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by zyrolasting
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.

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 enter

std::string line;
// the free-function std::getline() works with std::string
std::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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this