Question: i have the following code

```
void print(int & a, double & b, string & c)
{
    cout << setprecision(2) << fixed;
    const double GPA = a/b;
    if(c == "Y")
    {
        cout << "\n\nTotal number of credit hours: " << a << endl;
    }
    else
    {
       cout << "\n*** Grades are being held for not paying the tuition. ***" 
    }
 }

```

How can I write the `cout` in `print(int, double, string)` into a text file without tampering with `print(int, double, string);`? I tried something like this 

```
ofstream file;
file.open("file.txt");
file << print(a,b,c);
file.close();
cout << "file created" << endl;

```

but this doesn't compile. Why not, and how do I fix it?

Answer: 
bgoldst's answer solves the problem as asked, but I recommend a completely different solution. Stick your data in a class that has `operator<<` overloaded.

```
struct class_results {
    int credits;
    double GP_total;
    bool tuition_paid;
};
std::ostream& operator<<(std::ostream& out, const class_results& c) {
    if (c.tuition_paid) {
        const double GPA = c.credits/c.GP_total;
        out << "Total number of credit hours: ";
        out << setprecision(2) << fixed << c.credits<< '\n';
    } else
       out << "\n*** Grades are being held for not paying the tuition. ***" 
    return out;
}

```

Then usage is slightly more normal:

```
class_results results = {num_credits,GPTottal,tuition};
ofstream file;
file.open("file.txt");
file << results;
file.close();
cout << "file created" << endl;

```
> 
> How can I write the `cout` in `print(int, double, string)` into a text file without tampering with `print(int, double, string);`?
> 
> 
> 

**You can't.**

The function `print` is broken and you can't do what you want without fixing it.