Writing numbers from a generated list to a text file??

Hello, I am working on the following exercise to build my C++ skills:
"Write a program that will generate 1000 random numbers (all of which are between 10 and 20), and then write all of the even numbers out of that list of randoms to a new file called “myEvenRandoms.txt”."

This is the code I have so far. I'm aware that I have to use ofstream, but no matter what I tried I couldn't get it to work.

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

char analyze(int a)
{
	if (a % 2 == 0)
		return 'Even';
	else
		return 0;
}

void main()
{
	srand(time(0));
	for (int i = 0; i < 1000; i++)
	{
		int Numbers = rand() % 10 + 11;
		cout << Numbers << endl;
		char myChar = analyze(Numbers);
	}
}
Where is your code to open the file, output to the file stream and close the file?
I don't know where to put it / how to integrate it, the way I wrote it so far
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

char analyze(int a)
{
	if (a % 2 == 0)
		return 'Even';
	else
		return 0;
}

void main()
{
	//declare file stream
	ofstream fout;

	//open file
	fout.open("numbers.txt");

	//test if file opened
	if (fout.fail())
	{
		cout << "Error opening output file!" << endl;
	}
	else
	{
		srand(time(0));
		for (int i = 0; i < 1000; i++)
		{
			int Numbers = rand() % 10 + 11;
			fout << Numbers << endl;
			char myChar = analyze(Numbers);
		}
		cout << "Numbers exported to file numbers.txt" << endl;
	}

	//close file
	fout.close();
}


Hope this helps
Yes that helps a lot but how do I get it to only output the even numbers? This is the last part
Code thus far

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 <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

char analyze(int a)
{
	if (a % 2 == 0)
	{
		cout << a << endl;
		return 'Even';
	}
	else
	{
		cout << " " << endl;
		return 'Odd';
	}
}

void main()
{
	ofstream fout;
	fout.open("C:/Users/Zorai/Documents/numbers.txt");
	if (fout.fail())
	{
		cout << "Error opening output file!" << endl;
	}
	else
	{
		srand(time(0));
		for (int i = 0; i < 1000; i++)
		{
			int Numbers = rand() % 10 + 11;
			char myChar = analyze(Numbers);
			fout << Numbers << endl;
		}
		cout << "Numbers exported to file numbers.txt" << endl;
	}

	fout.close();
}
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
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>

int main() // main *must* have return type int
           // http://www.stroustrup.com/bs_faq2.html#void-main
{
    std::ofstream fout( "C:/Users/Zorai/Documents/numbers.txt" );

    if( !fout.is_open() ) std::cerr << "error opening file!\n" ;

    else
    {
        // consider using facilities in <random>
        // http://en.cppreference.com/w/cpp/numeric/random
        std::srand( std::time(nullptr) ) ;

        for( int i = 0; i < 1000;  ++i )
        {
            const int number = std::rand() % 10 + 11;
            if( number%2 == 0 ) fout << number << '\n' ; // if it is an even number
        }

        if(fout) std::cout << "numbers were written to file\n" ;
        else std::cerr << "error while writing numbers to file\n" ;
    }
}
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

void analyze(int Numbers, ofstream &fout)
{
	if (Numbers % 2 == 0)
	{
		fout << Numbers << endl;
	}
}

void main()
{
	ofstream fout;
	fout.open("C:/Users/Zorai/Documents/numbers.txt");
	if (fout.fail())
	{
		cout << "Error opening output file!" << endl;
	}
	else
	{
		srand(time(0));
		for (int i = 0; i < 1000; i++)
		{
			int Numbers = rand() % 10 + 11;
			analyze(Numbers, fout);
		}
		cout << "Numbers exported to file numbers.txt" << endl;
	}

	fout.close();
}
Topic archived. No new replies allowed.