Writing prime numbers to a file

I have tried a few different solutions to get this program to print all prime numbers 1-100 into a file. However, all things I have tried led to either nothing getting stored to the file, or all numbers getting stored in the file.
Not looking for the answer but I would greatly appreciate any tips/hints and/or guidance. Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//This program will write all prime numbers from 1-100 into a file

#include <iostream>
#include <fstream>

using namespace std;

//Function Prototype
bool isPrime(int);

int main()
{
   //Define ofstream object to be used in storing prime numbers
    ofstream outputPrime ("Prime_Numbers.txt");

    //For Loop runs through all numbers 1 - 100.
    for(int ctr = 1; ctr <= 100; ctr ++)
    {
        //Call to function isPrime
        isPrime(ctr);

        //If the function isPrime returned true print the ctr number to the file
        if(isPrime(ctr) == 1)
        {
            cout << ctr << endl;
            outputPrime << ctr << endl;;
        }
    }
      //close the ofstream object
        outputPrime.close();
}

//*****************************************
//This function will return a 1 if the    *
//number is prime.                        *
//*****************************************

bool isPrime(int ctr)
{
  for(int i = 1; i <= ctr; i++)
    {
        if(ctr % i == 0)
            return 0;
        else
            return 1;
    }
}

The main problem seems to be in your isPrime function, not the writing stuff.
The "else" statement in your for loop is the equivalent of saying "If the number is not divisible by 1, return true".. do you see why?

I hope that was enough of a hint since you said you didn't want the full answer.



Some other side stuff:

1
2
3
4
5
6
        isPrime(ctr); //This doesn't do anything! It simply runs the code and does nothing with it.

        //This is where you actually test the return value
        if(isPrime(ctr) == 1)
            ...
        }


Also, while "1" is true, and "0" is false, it would probably better for you to write:
return true; instead of return 1; and
return false; instead of return 0;

Third, saying
if (isPrime(ctr) == 1)
is the equivalent of saying
if (isPrime(ctr))
Last edited on
Thanks for the fast response. I am still unsure as to how to solve my issue. I feel like i need to remove the return 0 from the bool function but i don't know what to replace it with. Am I on the right track? Any ideas?
There are Three problems in your isPrime function.

The first problem is that you set i = 1. So why is this a problem? Because the remainder of any integer divided by 1 is zero.

ie. if(ctr % i == 0) will always evaluate to true when i = 1.

The second problem is that both your if and else statements execute a return statement. Having both if and else execute return statements ensures that a return statement will execute on every iteration of the loop. But a return statement ends a function so the loop will never execute more than once.

The third problem is in your loop condition i <= ctr;
this says that i and ctr can be equal the loop will execute but the remainder of any number divided by itself is 0 which means if(ctr % i == 0)will be true for all numbers.
Last edited on
Topic archived. No new replies allowed.