C++: how do I check if the cin buffer is empty?

后端 未结 4 429
梦谈多话
梦谈多话 2020-12-23 20:08

How do you check to see if the user didn\'t input anything at a cin command and simply pressed enter?

4条回答
  •  有刺的猬
    2020-12-23 20:53

    The Simple way >>

    {
    char X=0; //  ASCII( 0 ) means a NULL value
    cin>>X;
    if(X==0 || X==10) // ASCII( 10 ) means ENTER
    cout<<"User din't enter ANYTHING !! ";
    }
    

    But a simple problem is....

    cin just won't allow you to move further without entering a character

    by character here i mean a DIGIT or alphabet or special symbol , not space, enter null etc

    Hope this solves your problem, if it doesn't, I'll be glad to help just let me know.

提交回复
热议问题