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.

I'm thinking that part of your problem might be compiler specific. I can't get the same output from your code. However, there are some issues that I noticed. Reading the first line from each file shouldn't work right with the extraction operator(>>), since that will use the space as a delimiter and stop the extraction. Changing those 2 lines to getline's will fix that. Also you seem to be adding a space on a new line after the first file has finished. This should break the listing so that the last name is separated from the rest of the names. If that isn't the intent that line can be removed:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    string line;
    string line2;
    ifstream list1;
    ifstream list2;
    ofstream list3;
    list1.open("Text.txt");
    list2.open("Text1.txt");
    list3.open("file3.txt");
    getline(list1, line, '\n');
    getline(list2, line2, '\n');
    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');
    }
    while (!list2.fail())
    {
        list3 << line2 << endl;
        getline(list2, line2, '\n');
    }
    list1.close();
    list2.close();
    list3.close();
    return 0;
}

What compiler are you using? I tried both NetBeans and Cygwin and they both output the file the same way with the last two lines on the same line. The code on line 41 was my feebile attempt to try and create the newline for apperance purposes. I did have questions though. You mentioned that the code on lines 19 & 20 with the extraction operatior (>>) should not work, but why? I changed the code to what you suggested but I do not seem where it made a difference. Now I understand maybe it would make a difference in a more complex code, I just did not see it here.

I tested with VS2015 and Code::Blocks using Cygwin GCC. Perhaps the difference is OS specific. The standard default delimiters for the extraction operator include the space. This means that extraction should stop when it encounters a space. In your case, it should put the initials on a separate line for the first two names.

I understand, well thank you for your time and what you offered.

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.