Write a program that merges the integer numbers in two sorte
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
 #include <string>
 #include <fstream>
using namespace std;
int main() // driver method
 {
    string inputinFile1, inputinFile12, outfile; // local variables
    cout << \"Enter the Name of the File 1 : \"; // prompt to enter the name
    cin >> inputinFile1; // get the file name
    cout << \"Enter the Name of the File 2 : \"; // prompt to enter the name
    cin >> inputinFile12; // get the file name
    cout << \"Enter the Name of the Output File : \"; // prompt to enter the name
    cin >> outfile; // get the file name
   
    ifstream inFile1(inputinFile1); // files for reading
    ifstream inFile2(inputinFile12);
   
    ofstream fout(outfile); // file for writing
    if (!inFile1.good() || !inFile2.good() || !fout.good()) // check if the file could be opened and edited or not
    {
        cout << \"Error opening file(s)\" << std::endl;
        return 0;
    }
   int data1; // required initialisations
    int data2;
   inFile1 >> data1; // read the data
    inFile2 >> data2;
   while (!inFile1.fail() && !inFile2.fail()) // check for the fail condition
    {
        if (data1 <= data2) // check for the data and compare it
        {
            fout << data1 << endl; // write the data to outfile
            inFile1 >> data1; // read the next data from the input
        } else {
            fout << data2 << endl; // write the data to outfile
            inFile2 >> data2; // read the next data from the input
        }
    }
   while (!inFile1.fail()) // check for the fail condition
    {
        fout << data1 << endl; // write the data
        inFile1 >> data1; // read again
    }
   while (!inFile2.fail()) // check for the fail condition
    {
        fout << data2 << endl; // write the data
        inFile2 >> data2;// read again
    }
    cout << \"Successfully Completed the Execution.!\" << endl;
    inFile1.close(); // close all the files respectively
    inFile2.close();
    fout.close();
 }
 OUTPUT :
a) input1.txt :
2
 6
 10
 12
 17
 b) input2.txt :
4
 8
 9
 15
 c) out.txt :
2
 4
 6
 8
 9
 10
 12
 15
 17
 Enter the Name of the File 1 : input1.txt
 Enter the Name of the File 2 : input2.txt
 Enter the Name of the Output File : out.txt
 Successfully Completed the Execution.!
 Hope this is helpful.


