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<
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.
#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;
}
#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;
}
#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:
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
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 4 images