Replace a few of those \n's with endls, so that the cout stream gets flushed at every step.