void write_records(SalesRecordPtr head, int n) // Save records to file
{ char outfilename[MAX_FILE_NAME + 1]; // Name of file for output records
ofstream out; // Output file stream
cout << "\n\nI need the output file name." << endl;
open_output(out, outfilename); // Get file name & open file
if (out.good())
{ out << n << endl;
int i;
for(i = 0; i < n; i++)
{ head[i].write(out);
}
out.close();
}
else
{ cout << "\a\nUnable to save data!" << endl;
}
#include <list>
// substitute actual typename of course
void write_records(std::list<typename>& head) // Save records to file
{ char outfilename[MAX_FILE_NAME + 1]; // Name of file for output records
ofstream out; // Output file stream
cout << "\n\nI need the output file name." << endl;
open_output(out, outfilename); // Get file name & open file
if (out.good())
{
for(std::list<typename>::iterator pos(head.begin()), end(head.end); pos != end; pos++)
{
(*pos).write(out);
}
out.close();
}
else
{
cout << "\a\nUnable to save data!" << endl;
}
You could also make it a template function and pass two iterators so that it is more generic and supports other container types as well. I could make many other comments as well such as, prefer to overload operator<< instead of having a write member function for the object. Take a look at this article with regards to passing arrays or containers to functions. There are many formulas for accomplishing that depending on what your preferences are. http://cplusplus.com/forum/articles/20881/