I understand that cin.eof() tests the stream format. And while giving input, end of character is not reached when there is wrong in the input. I tested
cin.eof()
Use a direct test of the status of the stream with:
while (cin >> i) { ... }