Dear all,
I experienced an unexpected error when using getline() to get input from a file like this:
a.out < inputFile
Part of the code is shown below
........
while (true) {
cout << "Enter a command: ";
getline(cin, input);
cout << input << endl;
}
.........
The output is:
....
Enter a command:
Enter a command:
Enter a command:
Enter a command:
Enter a command:
Enter a command:
Enter a command:
Enter a command:
.....
It fails to get anything from the input file. But it runs without problem if the while loop is removed and enter 'a.out < inputFile'
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a command: ";
getline(cin, input);
cout << input << endl;
return 0;
}
What getline() behaves differently inside and outside a loop?
Jim