Change
old += s + "\n"; // because when s is added to old it is just a string not "\n" (the line break) is added.
So we need to add a line break by ourselves.
And secondly, there is no need to add "\n"
in out << old << "\n" << line;
Just change it to
out << old << line << "\n"; // line break at the end of each word is needed.
Hope it does your task. :)