Getline () does not work on standard redirected Linux


I am debugging a file on my computer. Rather than use keyboard input, I am running the program like so:

./main < input.txt

There is a section of code in my program that looks like this:

do {
    cout << "Type \"STOP\" when done: ";
    getline(cin, myString);
    cout << endl;
    if (myString == "STOP") {
    cout << "Input next line: " << endl;
    getline(cin, myString2);
    cout << endl;

    // do something with the two strings

} while (myString != "STOP");

Instead of stopping when there is a line in the file that equals "STOP" (no quotes), it keeps looping forever. Earlier, there is a cin >> myInt, but I made sure to use cin.ignore() after every call. It works fine if I type in the values without redirecting.

This is the output:

Type "STOP" when done:
Type "STOP" when done:
Type "STOP" when done:
Type "STOP" when done:
Type "STOP" when done:
Type "STOP" when done:
Type "STOP" when done:

Please help me figure out what is going on!

Ok, I figured it out! Like Basile Starynkevitch suggested, the problem was not with my code but with the input file. I wrote the input file on my Windows laptop and ran it from a Linux VM, and there was some confusion about the DOS line endings that caused the error.

Because Windows terminates their lines with \r\n and *nix terminates them with just \n, the cin.ignore() ignored the \r but not the \n, which was being read into the getline().

Solution: simply write the input file in Linux, or use the "unix" line endings in your editor of choice (usually something like View -> Line Endings -> Unix).

Curse you blasted "standards" developers!