我一直在试图弄清楚如何将程序输出镜像到 .txt 文件以及控制台。我对此比较陌生,所以我迷路了。请帮忙,这是我到目前为止所拥有的。
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
bool isPrime (int);
int main ()
{
int numbers;
ifstream inputFile;
//open the file
inputFile.open("22.txt");
//output to file
ofstream outFile;
outFile.open("PrimeNumbers.txt");
while(inputFile >> numbers)
if(isPrime(numbers))
outFile << numbers << endl;
cout << numbers << " is a prime number." << endl;
} //end of main function
//function formula
bool isPrime (int num)
{
int count=0;
for (int numb=2;
numb<=num/2;
numb++)
{
if(num%numb==0)
count++;
}
if (count>0)
return false;
else
return true;
}