So far this is what I have come up with for my program but I can't figure out why it wont compile correctly, I'm getting a lot of errors. Anyone have any suggestions? The program needs to merge male and female clients from two different files and output to another file. Thanks!
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inFile1;
ifstream inFile2;
ofstream outFile1;
int mClientNumber, fClientNumber;
string mClientName, fClientName;
bool atLeastOneFileNotAtEnd = true;
bool inFile1Written = false;
bool inFile2Written = false;
cout << "File merge processing starting" ;
inFile1.open("MaleClients.rtf");
inFile2.open("FemaleClients.rtf");
outFile1.open("MergedClients.rtf");
inFile1 >> mClientNumber;
inFile1 >> mClientName;
inFile2 >> fClientNumber;
inFile2 >> fClientName;
while ( atLeastOneFileNotAtEnd == true )
{
if (inFile1 == EOF)
{
if (inFile2Written == false)
{
outFile1 << fClientNumber << endl;
outFile1 << fClientName << endl;
inFile2Written = true;
}
else if (inFile2 == EOF)
if (inFile1Written == false);
{
outFile1 << mClientNumber << endl;
outFile1 << mClientName << endl;
inFile1Written = true;
}
else if (mClientNumber < fClientNumber)
outFile1 << mClientNumber << endl;
outFile1 << mClientName << endl;
inFile1Written = true;
else
outFile1 << fClientNumber << endl;
outFile1 << fClientName << endl;
inFile2Written = true;
}
if ((inFile1 ! EOF) && (inFile1Written == true))
{
inFile1 >> mClientNumber;
inFile1 >> mClientName;
inFile1Written = false;
}
if ((inFile2 ! EOF) && (inFile2Written == true))
{
inFile2 >> fClientNumber;
inFile2 >> fClientName;
inFile2Written = false;
}
if ((inFile == EOF) && (inFile2 == EOF))
{
atLeastOneFileNotAtEnd = false;
}
}
inFile1.close();
inFile2.close();
outFile1.close();
cout << "Merging Complete"
system("pause");
return 0;
}
The main problem is your way to check end-of-file.
if (inFile1 == EOF)
should be
if (inFile1.eof())
And somewhere you are using inFile
which is not defined before, it should be inFile1
.
Another problem is, it seems you have put an unnecessary ;
here
if (inFile1Written == false);
^
You are using the EOF
macro from C. There is no need to do this. Stick with C++ and use ifstream.eof()
, and get rid of the cstdlib
include.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.