printing output to a txt file

Hi,

I am having issues printing the prime numbers from the calPrimes function to a text file. It prints out fine on the screen, but only writes the first number in the text file. I'm also having a hard time printing the numbers that have been evaluated to a file or the screen.:
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
void PrimeClass::calPrimes(){

    int num=PrimeClass::getNum2();
    ofstream outdata;
    bool test=true;
    for ( int i = 2; i <= num; i++){

            for ( int j = 2; j <i; j++){

                if ( i % j == 0 ){
                    test=false;

                }


            }
            if (test){
            cout << i << "\n";
            outdata.open("prime.txt");
            outdata << i;
            }

            test=true;
        }
	outdata.close();

    }
Line 19 outdata.open("prime.txt"); should not be inside the loop. Move it to the top, before line 6.
Thank you so much!!
Topic archived. No new replies allowed.