Reading piped input with C++

后端 未结 2 741
萌比男神i
萌比男神i 2020-12-10 02:56

I am using the following code:

#include 
using namespace std;

int main(int argc, char **argv) {
    string lineInput = \" \";
    while(line         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 03:39

    When cin fails to extract, it doesn't change the target variable. So whatever string your program last read successfully is stuck in lineInput.

    You need to check cin.fail(), and Erik has shown the preferred way to do that.

提交回复
热议问题