How do I prevent a runaway input loop when I request a number but the user enters a non-number?

前端 未结 6 814
名媛妹妹
名媛妹妹 2020-12-11 09:03

I need to know how to make my cin statement not appear to \'remove\' itself if you input the wrong type. The code is here:

int mathOperator()
{
  using names         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-11 09:23

    I agree that a char is just as handy, since you can always cast to int, to answer your question as to why this is happening, when a cin input is exected as an int but a char is entered, the input is kept in the input stream for the duration of the loop, which is why it seems to "disappear."

    For more information: see the post from Narue at http://www.daniweb.com/forums/thread11505.html

提交回复
热议问题