You need to store what you're doing in displayStars in a variable. Make it return a string, and from that you can output the string into the text file.
Another approach. You could replace the cout in lines 26 and 27 with outputFile. Call function displayStars() after the file has been opened at line 15.
The problem then is that function displayStars() would have no idea what outputFile is, so the program would not compile. One solution to that would be to move the declaration of outputFile out of main(), place it in the global scope at the top of the code. Or to avoid the use of a global variable, instead pass by reference the outputFile as a third parameter to the function which uses it.