I have just finished a merge and sort code for two seperate files. My two inputs are:
File 1
Adams C
Benton A
Jones D
Haley A
King B
Margaret F
File 2
Barnes A
Charles B
Johnson C
William D
and it keeps doing this for my out put file:
Adams C
Barnes A
Benton A
Charles B
Johnson C
Jones D
Haley A
King B
Margaret F William D
I would like each name on a seperate line. I am new to this and it is driving me a little crazy.
My code up to date:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
string line2;
ifstream list1;
ifstream list2;
ofstream list3;
list1.open ("file1.txt");
list2.open ("file2.txt");
list3.open ("file3.txt");
list1 >> line;
list2 >> line2;
while ((!list1.fail()) && (!list2.fail()))
{
if (line <= line2)
{
list3 << line << endl;
getline (list1, line, '\n');
}
else
{
list3 << line2 << endl;
getline (list2, line2, '\n');
}
}
while (!list1.fail())
{
list3 << line << endl;
getline (list1, line, '\n');
}
list3 << " " << endl;
while (!list2.fail())
{
list3 << line2 << endl;
getline (list2, line2, '\n');
}
list1.close();
list2.close();
list3.close();
return 0;
}
Please help, thank you.