I am using the following code:
#include
using namespace std;
int main(int argc, char **argv) {
string lineInput = \" \";
while(line
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.
string lineInput;
while (cin >> lineInput) {
cout << lineInput;
}
If you really want full lines, use:
string lineInput;
while (getline(cin,lineInput)) {
cout << lineInput;
}