I am working on a homework assignment:
File Input to the program is the body of the letter. Keyboard Input to the
program is the name and address of the recipients. The file contains all the
letter except the names, which are denoted by #N#. This occurs exactly once in
the letter. The program should copy the file to the output file, until the #N#
is encountered. At this point, the program should ask the user for a name,
accept the name, place it in the file. Then the program should finish copying
the file to the output file. The main program has compiled-in file names for
the input file and output file. It opens the files with error checking, and
calls the function to do the work. A function should be defined to do the
work that accepts one input and one output file stream. Obtain the file names
from your instructor. The student should create files to test your program.
This is what I have managed to do so far:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void addNames(ifstream& inStream, ofstream& outStream);
int main()
{
ifstream fin;
ofstream fout;
fin.open("input-file.txt");
if (fin.fail())
{
cout << ("\n Error File not found");
exit(1);
/* If file cannot be opened the display
error message and exit. */
}
fout.open ("input-file-copy.txt",);
/* Open a file for writing ("w").*/
if (fout.fail())
{
cout << ("\n Error File not found");
exit(1);
}
addNames(fin, fout);
fin.close();
fout.close();
}
char next;
string n;
string l;
cout << "Please enter a first name and hit Enter: ";
cin >> n;
cout << "Please enter a last name and hit Enter: ";
cin >> l;
cout << "\n";
cin >> n >> l;
fin.get(next);
cin >> n;
while (! fin.eof())
if (next == '#N#')
fout << n;
else fout << next;
fin.get(next);
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
I keep getting compilation errors that I do not understand.