Problem Running Program After Compiling
I have written a program and it compiles successfully (or at least I am not getting any errors), but it will not run. I am sure I am missing whatever it is that I did wrong. Can someone take a look at this and please tell me what is wrong and why it's wrong, so I can avoid this in the future?
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <cstring>
using namespace std;
int openFile(fstream& f, fstream& f1, fstream& f2);//function to open the files
void readWriteData(fstream& f, fstream& f1, fstream& f2);//function to read and write data
int main()
{
fstream outFile; //output
fstream inFile1;//input
fstream inFile2;//input
openFile(outFile, inFile1, inFile2);
cin.get();
return 0;
}
int openFile(fstream& outFile, fstream& inFile1, fstream& inFile2)
{
char outName[50];//storage of user input filename for the data file that is to be written to
char inName1[50];//storage of user input filename for the first data file that is to be read from
char inName2[50];//storage of user input filename for the second data file that is to be read from
cout << "Please type the name of the first input file, including the extension: ";//requests user input of filename
cin >> inName1;
cout <<'\n';
inFile1.open(inName1, ios::in);
if (inFile1.fail())//if file cannot be opened, a message will display
{
cerr << "Could not open " << inName1 << "!" << endl;
return EXIT_FAILURE;
}
cout << "Please type the name of the second input file, including the extension: ";//requests user input of filename
cin >> inName2;
cout <<'\n';
inFile2.open(inName2, ios::in);
if (inFile2.fail())//if file cannot be opened, a message will display
{
cerr << "Could not open " << inName2 << "!" << endl;
return EXIT_FAILURE;
}
cout << "Please type the name of the output file, including the extension: ";//requests user input of filename
cin >> outName;
cout <<'\n';
outFile.open(outName, ios::out);
if (outFile.fail())//if file cannot be opened, a message will display
{
cerr << "Could not open " << outName << "!" << endl;
return EXIT_FAILURE;
}
readWriteData(outFile, inFile1, inFile2);
return 0;
}
void readWriteData(fstream& outFile, fstream& inFile1, fstream& inFile2)
{
string in1;
string in2;
string out;
while(!inFile1.eof() && !inFile2.eof())
{
inFile1 >> in1;
inFile2 >> in2;
if (strlen(in1.c_str()) < strlen(in2.c_str()))
{
in1 = out;
outFile << in1;
inFile1 >> in1;
}
else
{
in2 = out;
outFile << in2;
inFile2 >> in2;
}
if (!inFile1.eof() && !inFile2.eof())
{
do
{
inFile1 >> in1;
inFile2 >> in2;
outFile << in1;
outFile << in2;
}
while (!inFile1.eof() && !inFile2.eof());
}
}
inFile1.close();
inFile2.close();
outFile.close();
}