Reading a full line of input

后端 未结 5 1848
广开言路
广开言路 2020-11-27 06:27

I\'m trying to store the input that user enters through console. so I need to include the \"enter\" and any white space.

But cin stops giving me input a

5条回答
  •  我在风中等你
    2020-11-27 06:59

    is there a way like readLines till CTRL+Z is pressed or something ??

    Yes, precisely like this, using the free std::getline function (not the istream method of the same name!):

    string line;
    
    while (getline(cin, line)) {
        // do something with the line
    }
    

    This will read lines (including whitespace, but without ending newline) from the input until either the end of input is reached or cin signals an error.

提交回复
热议问题