detecting end of input with cin

前端 未结 6 1485
梦毁少年i
梦毁少年i 2020-12-10 06:13

I want to read a line of integers from the user. I\'m not sure how to check to see if the input has ended. For example I want to be able to do something like



        
6条回答
  •  甜味超标
    2020-12-10 06:55

    I usually detect end of cpp stream below:

    while (cin.peek() != EOF) {
      // To do your stuff...
      // NOTE: peek() won't set failbit and badbit when peeking end of stream.
      // So you could use cin.exception() instead of testing manually
      // fail() or bad().
    }
    

提交回复
热议问题