Seemingly Correct Program Crashing!

Hi!

I tried to write a program that outputs all numbers from 1 to 1000 that have prime factors which add up to a prime number (it's an exercise in the book Jumping Into C++)
I've finished the program, and it seems correct, but each time I run it I get an alert saying the program has stopped working, and then a line saying "Process Returned 255 (0xFF)" appears on the console. I use the Code::Blocks IDE, but I then tested it with Dev-C++ and VC++ and got the same errors.
I tried to find a solution to this elsewhere, but everything I found was for more complex programs that I didn't understand.

Here's the code:

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
#include <iostream>

using namespace std;

bool divisionTest(int num1, int num2);
bool primeTest(int num);

int main() {
    int factors;
    for(int i = 1; i <= 1000; i++) {
        factors = 0;
        for(int j = 0; j < i; j++) {
            if(divisionTest(i, j) == true) {
                if(primeTest(j) == true) {
                    factors += j;
                }
            }
        }
        if(primeTest(factors) == true) {
            cout << i << endl;
        }
    }
    return 0;
}

bool divisionTest(int num1, int num2) {
    if(num1 % num2 == 0) {
        return true;
    } else {
        return false;
    }
}

bool primeTest(int num) {
    for(int i = 0; i < num; i++) {
        if(i != 1 && num % i == 0) {
            return false;
        }
    }
    return true;
}


I know I've probably made a dumb error somewhere, but I can't find it!
Last edited on
Make sure the right operand of % is not zero.
The program runs now, thanks so much!

Always the little errors...
Topic archived. No new replies allowed.