Another newbie question here. What's the difference between these two?

cin.ignore(cin.rdbuf()->in_avail(), '\n');
cin.ignore(numeric_limits<streamsize>::max(), '\n');

I understand that the former will ignore only the characters left in the stream buffer, while the latter tries to ignore characters equal to the size of the buffer. So the end result should, normally, be the same. But then my question is why, when used in this function, the former explodes (infinite loop of "Invalid selection.\n") and the latter works fine?

int mainMenu(){
    int pick=0;
    
    cout<<"\n\nPlease make your selection:\n\n"<<
    "\t1) New game\n"<<
    "\t2) Options\n"<<
    "\t3) Quit\n\n";
    while (!pick){
        cin>>pick;
        if (pick>0 && pick<4) return(pick);
        else pick=0;
        if (!cin.good()){     //Clear mangled input
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); //This works, in_avail doesn't.
        }
        cout<<"Invalid selection.\n";
    }

 }

I'm using code somewhat above my understanding here. What exactly is rdbuf()->in_avail() returning when std::cin has been clobbered?

Another newbie question here. What's the difference between these two?

cin.ignore(cin.rdbuf()->in_avail(), '\n');
cin.ignore(numeric_limits<streamsize>::max(), '\n');

I understand that the former will ignore only the characters left in the stream buffer, while the latter tries to ignore characters equal to the size of the buffer. So the end result should, normally, be the same.

The difference is that only the second is guaranteed to be meaningful. In fact, on at least one popular implementation, in_avail() always returns 0.

Alright, so, essentially in_avail() is unreliable because there's no guarantee the OS has passed any or all characters to the process' stream buffer? Or i'm just not understanding how stream buffers work. Or something.

Thanks though, I'll stay away from in_avail() in the future.

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.