Hello everyone, I have just started learning C++ and I am trying to write a program that reads from a file (scores.txt) and then prints the average of these numbers to the screen and print to another file named "average.txt." I have written some code but when I compile I keep getting an error with the following lines (undefined identifier 'print'):
print(cout, temp); // print to screen
print(fout, temp); // print to file
Any help or tips would be appreciated. Thanks!
Complete code:
#include <iostream>
#include <fstream> // for file I/O
#include <cstdlib> // for exit
using namespace std;
void copyFile(ifstream&, ofstream&);
int main ()
{
ifstream in_stream;
ofstream out_stream;
in_stream.open("scores.txt");
out_stream.open("average.txt");
// This code checks for failures
if( in_stream.fail() || out_stream.fail())
{
cout << "Error opening file... exiting..." << endl;
// Exits the program
exit(1);
}
double next, sum = 0;
int count = 0;
// The code continues until the end of the file is reached
while(!in_stream.eof())
{
in_stream >> next; // get next number
sum = sum + next; // keeps track of the sum
count++;
}
// calculate and print average
double average = sum / count;
cout << "average = " << average;
copyFile(in_stream, out_stream);
// close both streams
in_stream.close();
out_stream.close();
return 0;
}
void copyFile(ifstream &fin, ofstream &fout)
{
char temp;
while(!fin.eof())
{
// read in one char including spaces
// and new lines!
fin.get(temp);
print(cout, temp); // print to screen
print(fout, temp); // print to file
}
}