I have been trying to figure out how to mirror the programs output to a .txt file as well as the console. I am relatively new to this, so I’m lost.. Please help, here is what I have so far.
#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;
}
One error that stands out to me is:
You don’t have any blocks (defined by curly brackets) so this snippet is equivalent to:
See how the “cout” statement isn’t affected by the condition? What you wanted to write is:
(Also, see my comment underneath your question requesting a more precise definition of the problem if you want more help)