Write a program that merges the numbers in two files and write all the numbers i
ID: 3631751 • Letter: W
Question
Write a program that merges the numbers in two files and write all the numbers into a third file.Your program takes input from two different files and writes it output to a third file.Each input file contains a list of numbers of type int in sorted order from the smallest to the largest.After the program is run,the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest.Your program should define a function that is called with the two input-file streams and the output-file stream as three arguments.create your own file names?and run your code?
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//These functions explained below
void spaces();
void introduction();
int main()
{
ifstream infile, infile2;
ofstream outfile;
int numbers,number;
char response;
do
{
infile.open("one.txt");
if(infile.fail())
{
cout << "I could not open thatfile.";
exit(1);
}
infile2.open("two.txt");
if(infile2.fail())
{
cout << "I could not open thatfile.";
exit(1);
}
outfile.open("both.txt");
if(outfile.fail())
{
cout << "I could not open thatfile.";
exit(1);
}
while(!infile.eof())
{
bool less=false;
infile >> number;
while(!infile2.eof())
{
infile2 >> numbers;
if(number < numbers)
{
outfile << number;
less = true;
}
if(less == true)
outfile << numbers;
}
}
infile.close();
infile.clear();
infile2.close();
infile2.clear();
outfile.close();
//Asks the user he would like to go again anything other than (Y ory) the program ends
cout << "Would you like to go again (Yor y): ";
cin >> response;
cout << endl;
}while(response == 'Y' || response== 'y');
return 0;
}
//This void function is for 2 statement spaces betweensentences
void spaces()
{
cout << endl << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.