Hello! Can you help to fix the problem in my program.!!! Here I did this program to merge two sorted files to third file. The Minfunk1.h should check if the files are sorted or not. (The wrong is that the function check just the two first numbers if they are sorted and it should check from the first number to the end) How can I fix it. The Minfunk2.h is a function for merging the two sorted files (The wrong is that third file is not sorted and it should be sorted) Minfunk1.h : #ifndef MINFUNK1_H #define MINFUNK1_H #include #include #include using namespace std; bool check(); #endif Minfunk1.cpp #include #include "minfunk1.h" //define the boolean function that returns true if the file are sorted otherwise false bool file(char fname[10]){ ifstream file; int a, b; file.open(fname); bool ch = true; while (file >> a >> b) { if (a > b) { return false; } a = b; } return true; } Minfunk2.h #ifndef MINFUNK2_H #define MINFUNK2_H #include #include "minfunk1.h" #include //namespace using namespace std; //define the boolean function that returns true if the files are sorted otherwise false bool check(char fname1[10], char fname2[10]); #endif Minfunk2.cpp #include #include //namespace using namespace std; //define the boolean function that returns true if the files are sorted otherwise false bool files(char fname1[10], char fname2[10]) { //objects of ifstream class ifstream file1, file2; //declare the variables to compare the integer values int a,b,c,d; //open the files file1.open(fname1); file2.open(fname2); //set the variable as true bool ch1 = true; //use the loop to iterate while(file1 >> a >> b && file2 >> c >> d) { //if the values are not sorted if(a>b || c>d) { //cout << "Both arrays are not sorted"; ch1 = false; } //swap the successor values in the preceding term a = b; c = d; } //returns true return ch1; } Main.cpp #include "minfunk1.h" #include "minfunk2.h" int main() { //Create the objects of ifstream class ifstream file1, file2; //Create the object of ofstream class ofstream file3; //to store the names of the files char fname1[100], fname2[100], fname3[100]; int ch; //Ask the user to enter and read the file names cout<<"Enter first file name :: "; cin>>fname1; cout<<"\nEnter second file name :: "; cin>>fname2; cout<<"\nEnter third file name :: "; cin>>fname3; //Open the files file1.open(fname1); file2.open(fname2); file3.open(fname3); //if the files are sorted if(check(fname1,fname2)) { cout << "The files are sorted "<< endl; //Store the content of file1 into file3 while(file1.eof()==0) { file1>>ch; file3<>ch; file3<
Hello!
Can you help to fix the problem in my program.!!!
Here I did this program to merge two sorted files to third file.
The Minfunk1.h should check if the files are sorted or not. (The wrong is that the function check just the two first numbers if they are sorted and it should check from the first number to the end) How can I fix it.
The Minfunk2.h is a function for merging the two sorted files (The wrong is that third file is not sorted and it should be sorted)
Minfunk1.h :
#ifndef MINFUNK1_H
#define MINFUNK1_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool check();
#endif
Minfunk1.cpp
#include <iostream>
#include "minfunk1.h"
//define the boolean function that returns true if the file are sorted otherwise false
bool file(char fname[10]){
ifstream file;
int a, b;
file.open(fname);
bool ch = true;
while (file >> a >> b)
{
if (a > b)
{
return false;
}
a = b;
}
return true;
}
Minfunk2.h
#ifndef MINFUNK2_H
#define MINFUNK2_H
#include <iostream>
#include "minfunk1.h"
#include <fstream>
//namespace
using namespace std;
//define the boolean function that returns true if the files are sorted otherwise false
bool check(char fname1[10], char fname2[10]);
#endif
Minfunk2.cpp
#include <iostream>
#include <fstream>
//namespace
using namespace std;
//define the boolean function that returns true if the files are sorted otherwise false
bool files(char fname1[10], char fname2[10])
{
//objects of ifstream class
ifstream file1, file2;
//declare the variables to compare the integer values
int a,b,c,d;
//open the files
file1.open(fname1);
file2.open(fname2);
//set the variable as true
bool ch1 = true;
//use the loop to iterate
while(file1 >> a >> b && file2 >> c >> d)
{
//if the values are not sorted
if(a>b || c>d)
{
//cout << "Both arrays are not sorted";
ch1 = false;
}
//swap the successor values in the preceding term
a = b;
c = d;
}
//returns true
return ch1;
}
Main.cpp
#include "minfunk1.h"
#include "minfunk2.h"
int main()
{
//Create the objects of ifstream class
ifstream file1, file2;
//Create the object of ofstream class
ofstream file3;
//to store the names of the files
char fname1[100], fname2[100], fname3[100];
int ch;
//Ask the user to enter and read the file names
cout<<"Enter first file name :: ";
cin>>fname1;
cout<<"\nEnter second file name :: ";
cin>>fname2;
cout<<"\nEnter third file name :: ";
cin>>fname3;
//Open the files
file1.open(fname1);
file2.open(fname2);
file3.open(fname3);
//if the files are sorted
if(check(fname1,fname2))
{
cout << "The files are sorted "<< endl;
//Store the content of file1 into file3
while(file1.eof()==0)
{
file1>>ch;
file3<<ch << " ";
}
//Store the content of file2 in the file3
while(file2.eof()==0)
{
file2>>ch;
file3<<ch << " ";
}
//Display the message
cout << "The items of " << fname1 << " and " << fname2 <<
" are copied to " << fname3 << " successfully" << endl;
}
//if any or both the arrays are not sorted
else
{
cout << "The files are not sorted";
}
return 0;
}
Two sorted files (File1.txt: 1 3 4 6 7 8 11) (File2.txt: 2 3 5 6 7 9 10 11 12 13)
The C++ program going to check if the file are sorted and merge them to third file (Mergefile.txt: 1 2 3 3 4 5 6 6 7 7 8 9 10 11 11 12 13)
Step by step
Solved in 6 steps with 1 images