Would you help me to fix the main.cpp : To write the results in the third file instead of in the compiler .  And if you can help with comment the program and the functions. Thanks.  main.cpp: #include #include #include "CheckSorted.h" #include "merge.h" using namespace std; int main() {     string file1;     string file2;     cout<<"Enter the first file name: ";     cin>>file1;     bool ans=check(file1);     if(ans){         cout<>file2;     ans=check(file2);     if(ans){         cout< #include using namespace std; bool check(string fileName){     ifstream in(fileName);     int a,b;     in>>a;     cout<<"\nChecking "<>b;         cout<b){             cout< #include using namespace std; void mergeIt(string file1,string file2){     ofstream out("Mergefile.txt");     ifstream in1(file1);     ifstream in2(file2);     int cnt1=0,a;     // count total values in file1     while(1){         in1>>a;         if(in1.eof()){break;}         cnt1++;     }     int cnt2=0,b;     // count total values in file2     while(1){         in2>>b;         if(in2.eof()){break;}         cnt2++;     }     in1.close();     in2.close();     ifstream in3(file1);     ifstream in4(file2);     int cnt;     if(cnt1>cnt2){         cnt=cnt1;     }     else{         cnt=cnt2;     }     while(cnt!=0){         in3>>a;         in4>>b;         if(cnt1>cnt2){         if(!in4.eof()){         if(a>b){             out<a){             out<cnt1){         if(!in3.eof()){         if(a>b){             out<a){             out<b){             out<a){             out<

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Would you help me to fix the main.cpp : To write the results in the third file instead of in the compiler .  And if you can help with comment the program and the functions. Thanks. 

main.cpp:

#include <iostream>
#include<fstream>
#include "CheckSorted.h"
#include "merge.h"
using namespace std;

int main()
{
    string file1;
    string file2;
    cout<<"Enter the first file name: ";
    cin>>file1;
    bool ans=check(file1);
    if(ans){
        cout<<endl<<file1<<" is sorted."<<endl;
    }else{
        cout<<endl<<file1<<" is not sorted."<<endl;
        exit(1);
    }

    cout<<"\nEnter the second file name: ";
    cin>>file2;
    ans=check(file2);
    if(ans){
        cout<<endl<<file2<<" is sorted."<<endl;
    }else{
        cout<<endl<<file2<<" is not sorted."<<endl;
        exit(1);
    }
    mergeIt(file1,file2);
    return 0;
}

CheckSorted.h:

#include<iostream>
#include<fstream>
using namespace std;
bool check(string fileName){
    ifstream in(fileName);
    int a,b;
    in>>a;
    cout<<"\nChecking "<<fileName<<" is sorted or not."<<endl;
    while(1){
        if(in.eof()){break;}
        in>>b;
        cout<<a<<" "<<b<<endl;
        if(a>b){
            cout<<a<<" is greater than "<<b<<". So, file is not sorted."<<endl;
            return false;
        }
        a=b;
    }
    in.close();
    return true;
}

merge.h:

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

void mergeIt(string file1,string file2){

    ofstream out("Mergefile.txt");
    ifstream in1(file1);
    ifstream in2(file2);

    int cnt1=0,a;
    // count total values in file1
    while(1){
        in1>>a;
        if(in1.eof()){break;}
        cnt1++;
    }

    int cnt2=0,b;
    // count total values in file2
    while(1){
        in2>>b;
        if(in2.eof()){break;}
        cnt2++;
    }

    in1.close();
    in2.close();

    ifstream in3(file1);
    ifstream in4(file2);


    int cnt;
    if(cnt1>cnt2){
        cnt=cnt1;
    }
    else{
        cnt=cnt2;
    }

    while(cnt!=0){
        in3>>a;
        in4>>b;
        if(cnt1>cnt2){
        if(!in4.eof()){
        if(a>b){
            out<<b<<" ";
            out<<a<<" ";
        }
        else if(b>a){
            out<<a<<" ";
            out<<b<<" ";
        }
        else{
            out<<a<<" ";
            out<<b<<" ";
        }
        }
        else{
            out<<a<<" ";
        }
        }
        else if(cnt2>cnt1){
        if(!in3.eof()){
        if(a>b){
            out<<b<<" ";
            out<<a<<" ";
        }
        else if(b>a){
            out<<a<<" ";
            out<<b<<" ";
        }
        else{
            out<<a<<" ";
            out<<b<<" ";
        }
        }
        else{
            out<<b<<" ";
        }
        }
        else{
            if(a>b){
            out<<b<<" ";
            out<<a<<" ";
        }
        else if(b>a){
            out<<a<<" ";
            out<<b<<" ";
        }
        else{
            out<<a<<" ";
            out<<b<<" ";
        }
        }
        cnt--;
    }
    in3.close();
    in4.close();
    out.close();
}

 

The Output :

The file1.txt , File2.txt are sorted. The Merge results you can se in Mergefile.txt. 

File1.txt, File2.txt, Mergefile.txt:

File1.txt:

1 3 4 6 7 8 11

File2.txt :

2  3 5 6 7 9 10 11 12 13

Mergefile.txt:

1 2 3 3 4 5 6 6 7 7 8 9 10 11 11 12 13 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 4 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education