The following code simply echoes the standard input to the standard output. If I run the program like so ./a.out
, I can type anything and the program works fine. However, if I run it like this ./a.out < input.txt
I get an infinite loop, regardless of the content of input.txt
.
#include <iostream>
using namespace std;
int main() {
string input;
while (true) {
cout << "Type your input: ";
getline(cin, input);
cout << input << endl;
}
return 0;
}
What am I doing wrong?
EDIT: To clarify, I expect that after the input from the input file is finished, getline waits for more input from stdin. Instead, it continues to read when nothing is there.