Spitting out fizzbuzz to a txt file?

Quick question guys, I'm trying to make my whole fizzbuzz information print to a txt file. How would I go about implementing this?
My current way I have it coded has no errors BUT does not show anything in the txt file I generated. I understand to add the << to write to a file but how would you go about that with this type of program??
Thanks for the help.

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
50
51
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int lines = 1;

    //Writing to a file
    ofstream myInfo;
    myInfo.open("FizzBuzz.txt");

    while(lines <= 200)
    {
        int check = 0 + lines;

    for(int i = 1; i <= 100; i++)
    {
        if(i % 15 == 0)
        {
            cout << "FizzBuzz" << endl;
        }

        else if(i % 5 == 0)
        {
            cout << "Fizz" << endl;
        }

        else if(i % 3 == 0)
        {
            cout << "Buzz" << endl;
        }

        else
        {
            cout << i << endl;
        }


    }
        ++lines;
    }

    //closing file
    myInfo.close();
    cout << "\nDone Writing to file!" << endl;

    return 0;
}
 
Such a easy solution, just needed to think outside the box.

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

using namespace std;

int main()
{

    //Writing to a file
    ofstream myInfo;
    myInfo.open("FizzBuzz.txt");

    for(int i = 1; i <= 100; i++)
    {
        if(i % 15 == 0)
        {
            myInfo << "FizzBuzz" << endl;
        }

        else if(i % 5 == 0)
        {
            myInfo << "Fizz" << endl;
        }

        else if(i % 3 == 0)
        {
            myInfo << "Buzz" << endl;
        }

        else
        {
            myInfo << i << endl;
        }


    }

    //closing file
    myInfo.close();
    cout << "Done Writing to file!" << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.