Hello, I'm supposed to write a programming project that reads 2 files and then organizes the numbers into an output file in order from smallest to largest.

The input files are as they appear:

1
4
5

and also

2
3
6

Right now I'm having trouble with the output and my monitor only shows the numbers 1-4.
My code is:

If anyone can help that'd be great, thank!

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

void combine_file(ifstream& infile1, ifstream& infile2, ofstream& outfile);
//Combines intergers from both instream files and sorts them
//infile1 is the first list of integers
//infile2 is the second list of integers
//outfile lists all integers from both instream files in ascending order

int main()
{
   ifstream fin, fin2;
   ofstream fout;
   
            fin.open("input1.txt");
            if (fin.fail())
             {
                     cout << "Input file opening failed.\n";
                     exit(1);
             }
   
            fin2.open("input2.txt");
            if (fin.fail())
             {
                     cout << "Input file opening failed.\n";
                     exit(1);
             }
   
            fout.open("output.txt");
            if (fout.fail())
             {
                    cout << "Output file opening failed.\n";
                    exit(1);
             }
   
             combine_file(fin, fin2, fout);
             
              fin.close(); 
               fin2.close(); 
               fout.close();
               
             system("PAUSE");
           return 0;
   
}

void combine_file(ifstream& infile1, ifstream& infile2, ofstream& outfile)
{
     int c, d, store; 
      
     infile1 >> c;
     infile2 >> d; 
     
     while((!infile1.eof()) && (!infile2.eof()))
     {
             if (c < d)
               store = 1;
             else if (d < c)
               store = 2;
             else if (d == c)
               store = 3;
              
        switch (store)
        {
             case (1):
              {
                      cout << c << " "; 
                      infile1 >> c;
                       outfile << c << " ";
                       break;
              }
             case (2):
              {
                      cout << d << " ";
                      infile2 >> d;
                      outfile << d << " ";
                      break;
              }
             case (3):
              {
                      cout << c << " " << d << " ";
                      outfile << c << " " << d << " ";
                       infile1 >> c;
                       infile2 >> d;
                       break;
              }
       
             default:
                  cout << "Error";
                  
         }
        
      }
      
         while(!infile1.eof())
         {
              cout << c << " ";
              outfile << c << " ";
              infile1 >> c;
              break;
         }
         
         while(!infile2.eof())
         {
             cout << d << " ";
             outfile << d << " ";
             infile2 >> d;
             break;
         }
        
      cout << endl;
}

Recommended Answers

All 4 Replies

I think you would be better of reading both files into an array or vector and then sort the container. After that then write the data back into the outfile.

void CombineFile(ifstream& infile1, ifstream& infile2, ofstream& outfile)
{
    std::vector<int> numbers;
    //...
    // read in data from both files into the vector
    //...
    std::sort(numbers.begin(), numbers.end());
    //...
    // write the vector into outfile
    //...
}

Nathan is partially correct. Based on your code and assumed knowledge
1) read file 1 into an array.
2) read file 2 into an array.
3) merge the 2 arrays into a third.
4) write the 3rd array into file 3
5) Do not use sort in C++ because I'm sure your instructor wants you to learn the technique, not the C++ command.

Step 3 is the complicated one.
1) Start the indicies for array1 and array2 at 0
2) Compare the elements in array 1 and 2 based in the indecies.
3) Move the smaller (say array1) into array3. Move to the next element in array1.
4) Repeat 2&3 until all elements have been moved.

You'll have to figure out what to do when you get to the end of one of the arrays.

The only problem with that is we have yet to learn arrays in class so I'm assuming we can't use them. Are there any alternate possibilities? Sorry for the trouble!

Then in that case,
1) read one value from both files.
2) compare them
3) write the smallest to file3
4) read from the file that had the smallest value
5) loop back to 2 (do NOT use goto)

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.