Why all the explicit calls to flush? Those aren't typically needed, unless you either need immediate feedback on the console (unlikely) or to prevent too much output from getting buffered (& consuming RAM).