When I want to save result to file I use ofstream, right?

ofstream myout; 
myout.open (sav_fl, fstream::app);

But depending on the "ol" value(=user input),
I want to show it to screen not to file. How can I do?
I thought this way. But error message shown up.

if (ol == 1) myout = std::cout; // My code

"error: no match for ‘operator=’ in ‘myout = std::cout’
/usr/include/c++/4.4/iosfwd:84: note: candidates are: std::basic_ofstream<char, std::char_traits<char> >& std::basic_ofstream<char, std::char_traits<char> >::operator=(const std::basic_ofstream<char, std::char_traits<char> >&)"

And how can I use it in sentence?
I thought this way.

myout << "this is test" << endl;   //My code, is it right?

Thanks you guys..

You need to make it a reference.
In fact do something like this :

void write(const string& str, std::ostream& out = cout){
 out << str << endl;
}

Now you can call the code like this :

write("Hello"); // and prints to screen
write("Hello",myout); //and write to file
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.