Executing the command:
./program < input.txt
with the following code checking:
string input;
while(cin) {
getline(cin, input);
}
The above code seems to generate an extra getline() call where input is empty. This happens regardless of whether or not there’s a \n on the last line of input.txt.
@Jacob had the correct solution but deleted his answer for some reason. Here’s what’s going on in your loop:
cinis checked for any of the failure bits (BADBIT, FAILBIT)cinreports no problem because nothing has yet been read from the file.getlineis called which detects end of file, setting the EOF bit and FAILBIT.You need to do something like this instead: