//libraries
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
//function prototypes
void printmessage (ofstream &, int);
void printprimes (ofstream &, int);
bool isprime (int);
//enter the main
int main ()
{
ifstream in;
ofstream out;
//open data file
in.open("in.txt");
//if not open give error message
if (!in)
{
cout << "Did not open in.txt" <<endl;
return 1;
}
//open output file
out.open("out.txt");
//if not give error message
if (!out)
{
cout << "Did not open out.txt" << endl;
return 1;
}
// local variables
int input, n;
//priming read
in >> input;
//start of the loop
while (in)
{
//function
printmessage (out,input);
if (isprime (input))
// EOF
in >> input;
}
cout << "THE END." << endl;
//close the file
in.close();
out.close();
system ("pause");
return 0;
}
//Output
void printmessage (ofstream & out, int input)
{
out <<endl;
out << "The number is " << input;
out <<". The prime numbers less than or equal to " << input << " are: " << endl;
}
void printprimes (ofstream & out, int n)
{
cout << n << " ";
out << n <<" ";
}
bool isprime (int n)
{
int realnum, divisor, count;
ofstream out;
for (realnum=2; realnum <= n; realnum++)
{count = 0;
for (divisor=2; divisor < sqrt(n); divisor++)
{if (realnum % divisor == 0 && realnum != divisor)
{count = 1;
}
}
if (count == 0)
{
printprimes (out, realnum);
}
}
}
now my problem is it wont output to file, but it will to screen. It is probably something really simple, but i have been staring at it for 3 days and can't figure it out.

Any help will be appreciated. thanks, i'm a new user