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: 
The way you've written it, your `print()` function is not capable of outputting to any given stream. This is because it hard-codes the stream that it writes to as `cout`.

If you want it to be able to write to any given stream, you have to parameterize the stream as another function parameter. For (1) convenience and (2) compatibility with existing code that assumes `print()` only takes three arguments and writes to `cout`, you can make the new parameter optional by defaulting it to `cout`:

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

```

Then you can call it as follows:

```
print(a,b,c,file);

```

The reason why your code doesn't compile is that you cannot pass void as a function argument or operator operand. When a function is declared as returning void, that means *it does not return anything at all*. There is no data returned by `print()` to stream to the ofstream. The streaming takes place inside the function, so it is only there that you can select the stream to which the output will be written.
> 
> 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.