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
	}
}

I've never used the print command to do what you are trying to do. I use cout>>temp; which could work.

Also, to print to the file, you can open a stream, and use it to print to a file.


ifstream read_file;
ofstream write_file;

ifstream is in file stream
ofstream is out file stream


write_file.open(partNum.c_str()); // will open a file for writing with file name

write_file<<partNum2; // will write any data you want to the scree.

Use cout to print it to the screen.

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.