What is wrong with my source file that it will not compile
What is wrong with my source file that it will not compile?
// Is Prime Function
// Author: Lila Bailey
// Date: 11/03/2016
#include \"time.h\"
#include <iostream>
#include <fstream>
using namespace std;
// Function Prototype
bool isPrime(int);
int main()
{
// Variable declaration
fstream outfile(int);
int num;
// Open an output file
outfile.open(\"primesBefore100.txt\")
//Repeat loop 100 times
for (int num = 1; num <= 100; num++)
{
//call isPrime function with each variable
if (isPrime(num))
outfile << num << endl;
}
//close output file
outfile.close(int);
system(\"pause\");
return 0;
}
//**************************************************************
// The isPrime function determines whether a number is prime. *
// It displays \"Is a prime number\" or \"Is not a prime number\" *
//**************************************************************
bool isPrime(int num)
{
int count = 0;
for (int i = 2; i <= num / 2; i++)
{
if (num%i == 0)
count++;
}
if (count > 0)
return false;
else
return true;
}
Solution
Hi,
I have fixed the issue It is working fine now and highlighted the code changes beow.
#include \"time.h\"
#include <iostream>
#include <fstream>
using namespace std;
// Function Prototype
bool isPrime(int);
int main()
{
// Variable declaration
ofstream outfile;
int num;
// Open an output file
outfile.open(\"primesBefore100.txt\");
//Repeat loop 100 times
for (int num = 1; num <= 100; num++)
{
//call isPrime function with each variable
if (isPrime(num))
outfile << num << endl;
}
//close output file
outfile.close();
system(\"pause\");
return 0;
}
//**************************************************************
// The isPrime function determines whether a number is prime. *
// It displays \"Is a prime number\" or \"Is not a prime number\" *
//**************************************************************
bool isPrime(int num)
{
int count = 0;
for (int i = 2; i <= num / 2; i++)
{
if (num%i == 0)
count++;
}
if (count > 0)
return false;
else
return true;
}
Output:
primesBefore100.txt
1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97


