I'm going to guess that your function outputs to cout. In the absence of information, I have to guess. You can replace the streambuf associated with cout. Then, you can get the data from that buffer. In this example, the function outputting the number 1 2 3 4 5 6 is in the middle. Instead of going to screen, the output is put into the buffer, which is then dumped to a string and after cout is restored, output to screen. You could easily put it into an array.