Error while flushing and closing output terminate called after throwing an instance of 'int'