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.

Please tell us what errors you are getting.

Well, there was an erroneous comma on line 28.
Everything from line 42 on is outside of your main function. I'm guessing that is supposed to be the function definition of void addNames? I rearranged a few things:

#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();      
  system("PAUSE");
  return EXIT_SUCCESS;
}
 
void addNames(ifstream& inStream, ofstream& outStream)
{ 
char next;
string n;
string l;
 
       cout << "Please enter a first name and hit Enter: "<<endl;
       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);
 

}

You're gonna get an error that says fin and fout was not declared in this scope. Just move your declarations (ifstream fin and ofstream fout) before your main function to give them global scope. Hope this helps!

commented: Though it means nothing, I give you a thumbs up! +0

If you adjust your function's signature to: void addNames(ifstream& fin, ofstream& fout) (the parameters used in the function body must match those in the signature)
you can avoid the global variables.

Oh yeah, didn't even think of that...lol

Thank you for the help, but I am still getting weird errors. The compiler keep telling me that my comparison is always false due to limited range of data type. This is what my new code look like

#include<cstdlib>
#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;

void addNames(ifstream& fin, ofstream& fout);

int main(){
    ifstream fin;
    ofstream fout;
    
    fin.open("input-file.txt");
    
    if(fin.fail()){
                   
                  cout << ("\n Error File Not Found");
                  exit(1);
}


    fout.open("input-file-copy.txt");
    
    if(fout.fail())
    {
           cout << ("\n Error File Not Found");
           exit(1);
}

addNames(fin, fout); 
fin.close();
fout.close();
//system("PAUSE");
//return EXIT_SUCCESS;
}

void addNames(ifstream& fin, ofstream& fout){
     char next;
     string n;
     string l;
    
    
            cout << "Please enter a first name and hit Enter: " << endl;
            cin >> n;
            cout << "Please enter a last name and hit Enter: " << endl;
            cin >> l;
            cout << endl;
            cout << n << l << endl;
     
     fin.get(next);
     //cin >> n;
     
         while (! fin.eof())
               if (next == '#N#')
               fout << n;
               
               else fout << next;
         fin.get(next);
        
         
         }
{         
system("PAUSE");
return EXIT_SUCCESS; 
}

if (next == '#N#') '#N#' is not valid. Single quotes are for a single character (but can hold escape sequences like '\n' for newline). Make next a string, use a getline to read it in and then compare it with "#N#" instead.

Also, don't use feof. Use your getline(fin,next) (see first paragraph) to drive the loop.

Post the freakin' errors. We can't help you if you keep the errors a secret.

Post the freakin' errors. We can't help you if you keep the errors a secret.

I've already said that :P
In anycase, we need you to post the error that you are getting so we can figure out where exactly you are having problems. Just copy the selections or (right click, copy) and then paste them from your compiler to this website.

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.