Sorting Input files

I've got a program that needs to read in 3 input files with integers out of numeric order and I need to read in all three and sort them numerically then after the sort merge them to one ouput file...I've been working on this thing all weekend long and have gotten nowhere it seems...Any and All help is much appreciated! here's what I've got so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 #include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

ofstream outfile;
ifstream infile;

void bubbleSort(int arr[], int n) {

      bool swapped = true;
      int j = 0;
      int tmp;
      while (swapped) {
             swapped = false;j++;
            for (int i = 0; i < n - j; i++) {
                  if (arr[i] > arr[i + 1]) {
                        tmp = arr[i];
                        arr[i] = arr[i + 1];
                        arr[i + 1] = tmp;
                        swapped = true;}
                  }

            }

      }

int main()
{
    int x;
    outfile.open ("outfile.out");
    if (!outfile){
    cout << "You're doing it wrong!" << endl;
    return 1;}

    infile.open("inputdata1.in");
    if (!infile){
    cout << "You're doing it wrong!" << endl;
    return 1;}

    infile.open("inputdata2.in");
    if (!infile){
    cout << "You figure you'd have this down by now...guess not" << endl;
    return 1;}

    infile.open("inputdata3.in");
    if (!infile){
    cout << "Give it up...it's over he doesn't accept programs that don't work!" << endl;
    return 1;}


    infile >> x;
    while (infile)
    {
        bubbleSort();
    }


    outfile << endl;

    outfile.close();
    infile.close();
    cout << "Finish" << endl;

    return 0;

}
You can only read one file at a time. I recommend you use a function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <fstream>
#include <iostream>
using namespace std;

//-----------------------------------------------------------------------------
// Sort an array 'arr' of 'n' elements in non-decreasing order.
//
void bubbleSort(int arr[], int n)
{
    // this function is fine
}

//-----------------------------------------------------------------------------
// Append the contents of a text file of integers to the argument array.
// On input, '*n' indicates the initial size of the array.
// On output, '*n' indicates the final size of the array.
// It is presumed that the array can accomodate the size of the file.
//
void readFile(const char* filename, int arr[], int* n)
{
    ifstream infile( filename );
    if (!infile)
    {
        cerr << "I could not open the file " << filename << "\n";
        return;
    }

    // This is where your code to read the integers in infile into your array.
    // Remember to append the data to your array. Hence, if you succeed in
    // getting an integer from the file, first increment '*n', then make 'arr[*n]' = the
    // value you read.
    ...
}

//-----------------------------------------------------------------------------
int main()
{
    // Remember, you need somewhere to put your integers.
    // Make sure that your array is big enough to hold all the integers you will read.
    int xs[ 1000 ];
    int number_of_xs = 0;

    // This is where you read each file to append the integers to the end of your array.
    readFile( "inputdata1.in", xs, &number_of_xs );
    readFile( "inputdata2.in", xs, &number_of_xs );
    readFile( "inputdata3.in", xs, &number_of_xs );

    // Now sort your array
    bubbleSort( xs, number_of_xs );

    // Create the output file...
    ofstream outfile( "outfile.out" );
    if (!outfile)
    {
        cerr << "I could not create the file output.out\n";
        return 1;
    }

    // Here is where you write your sorted array back to file
    for (int n = 0; n < number_of_xs; n++)
      outfile << xs[ n ] << endl;

    // All done!
    return 0;
}

Make sure you use consistent indentation. It makes a huge difference when you read and think about your code. Particularly, the way you do lines like 33..35, etc, is confusing. Compare the way it looks on lines 22..26 of my code.

Hope this helps.
This format helps a ton...I appreciate it but now i cant get it to output anything to my outfile.
Topic archived. No new replies allowed.