Hello all. I'm currently writing a program in C++ that merges two text documents into one. I would have simply used the C++ merge function, but I was told to write the whole function out. The problem is, I can't get my code to compile. I'm sure it's just a stupid error involving the file opening lines (Ex: inFile1.open(inFileName1); ) but I have no idea on how to fix it. Any help would be graciously appreciated. Thanks!
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void sortTwoFiles(ifstream& in1, ifstream& in2, ofstream& out);
int main ()
{
ifstream inFile1;
ifstream inFile2;
ofstream outFile;
string inFileName1;
string inFileName2;
string outFileName;
cout<< "Enter the first input file's name: ";
cin >> inFileName1;
cout << "Enter the second input file's name: ";
cin >> inFileName2;
cout << "Enter the output file's name: ";
cin >> outFileName;
inFile1.open(inFileName1);
if(inFile1.fail())
{
cout << inFileName1 << " first file is not opened!"
<< endl;
system ("pause");
exit(1); }
inFile2.open(inFileName2);
if(inFile2.fail())
{
cout << inFileName2 << " second file is not opened!"
<< endl;
system ("pause");
exit(1); }
outFile.open(outFileName);
if(outFile.fail())
{
cout << outFileName << " output file is not opened!"
<< endl;
system ("pause");
exit(1); }
sortTwoFiles(inFile1, inFile2, outFile);
inFile1 .close();
inFile2.close();
outFile.close();
system("pause");
return 0;
}
void sortTwoFiles(ifstream& in1, ifstream& in2, ofstream& out)
{
int integer1;
int integer2;
in1 >> integer1;
in2 >> integer2;
while(!in1.eof()&& !in2.eof())
{
if(integer1 <= integer2)
{
out << integer1 << endl;
in1 >> integer1;
}
else
{
out << integer2 << endl;
in2 >> integer2;
}
if(integer1 <= integer2)
{
while(!in2.eof())
{
out << integer2 << endl;
in2 >> integer2;
}
}
else
{
while(!in1.eof())
{
out << integer1 << endl;
in1 >> integer1;
}
}
}
}