# Thread: Function-change ouput to a file

1. ## Function-change ouput to a file

I hope I did the code tags correctly:
1st I need to change the output to a file
2nd additional programming to find the sum and average of the number
3rdchange print result to print to a file
This is not an assigned homework just another example I want to try to figure out.
//code
Code:
```//Program: Classify Numbers
//This program counts the number of zeros, odd, and even numbers

#include <iostream>
#include <iomanip>

using namespace std;

const int N = 20;

//Function prototypes
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& num);
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);

int main ()
{
//Variable declaration
int counter; //loop control variable
int number;  //variable to store the new number
int zeros;   //variable to store the number of zeros
int odds;    //variable to store the number of odd integers
int evens;   //variable to store the number of even integers

initialize(zeros, odds, evens);                 //Step 1

cout << "Please enter " << N << " integers."
<< endl;                                   //Step 2
cout << "The numbers you entered are: "
<< endl;

for (counter = 1; counter <= N; counter++)      //Step 3
{
getNumber(number);                          //Step 3a
cout << number << " ";                      //Step 3b
classifyNumber(number, zeros, odds, evens); //Step 3c
}// end for loop

cout << endl;

printResults(zeros, odds, evens);               //Step 4

return 0;
}

void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}

void getNumber(int& num)
{
cin >> num;
}

void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount)
{
switch (num % 2)
{
case 0:
evenCount++;
if (num == 0)
zeroCount++;
break;
case 1:
case -1:
oddCount++;
} //end switch
}

void printResults(int zeroCount, int oddCount, int evenCount)
{
cout << "There are " << evenCount << " evens, "
<< "which includes " << zeroCount << " zeros"
<< endl;

cout << "The number of odd numbers is: " << oddCount
<< endl;
}```
//code

2. And what is the problem ? What's your question ?

3. >> I hope I did the code tags correctly:
Obviously not. Use the "Go Advanced" and "Preview Post" buttons to check before you make a reply.

For now, you can edit your post (click the "Edit" button at the bottom of your post). You need to put the [CODE] At the top of the code and the [/code] at the bottom. Make sure the [ in front of CODE is there. Then keep previewing until it looks correct, something like this:

Code:
`  // All my code here.`
Edit: It looks like you were given explicit instructions on how to use code tags already in another thread. Please make an effort to make things easier on those of us try to help.

5. I dont see an issue in it, very nicely done

6. Yes Thank you but what is some code to change output to a file? Assign a file and have a file name already set up--what type of extension does it need to be?
Where in the code is it best to get th?e totals

7. You can give a file any extension you like. You might want to make it a .txt if you want to notepad to open it for you by default tho.

Heres an example of how to write to a file in c++:
Code:
```#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ofstream afile;
afile.open("example.txt");
afile << "Gdnfsbfbkjdbd dgbfsdb.\n";
afile.close();
}```

8. To give the option to print to the screen or a file you could change printResults() to this:
Code:
```void printResults(ostream &out, int zeroCount, int oddCount, int evenCount)
{
out << "There are " << evenCount << " evens, "
<< "which includes " << zeroCount << " zeros"
<< endl;

out << "The number of odd numbers is: " << oddCount
<< endl;
}```
The if you want to print to the screen:
Code:
`    printResults(cout, zeros, odds, evens);               //Step 4`
And to a file:
Code:
```#include <fstream>
.
.
string filename = "file.txt";
ofstream out(filename.c_str());
if (!out.is_open())
{
cout << "Unable to open file: " << filename << endl;
return 1;
}
printResults(out, zeros, odds, evens);               //Step 4```
You can even print to both:
Code:
```#include <fstream>
.
.
printResults(cout, zeros, odds, evens);               //Step 4

string filename = "file.txt";
ofstream out(filename.c_str());
if (!out.is_open())
{
cout << "Unable to open file: " << filename << endl;
return 1;
}
printResults(out, zeros, odds, evens);               //Step 4```