Language C++ Givens: Key Program: OPmcopy key-fileMerge.run ./key-fileMerge.run
ID: 3877223 • Letter: L
Question
Language C++ Givens: Key Program: OPmcopy key-fileMerge.run ./key-fileMerge.run Sample function: InFileValueCount.cpp NOTE: YOUR PROGRAM must behave EXACTLY LIKE key-fileMerge.run!! Description: Design and write a program that merges the contents of two sorted input files containing integer values into a single sorted output file. Required Input/Output Formats: Input Prompt: Enter Name of Input File 1: Enter Name of Input File 2: Enter Name of Output File: Output labels/format: (x marks define field width) INPUT FILE1 'infilename1' : CONTAINS xxx VALUES INPUT FILE2 'infilename2' : CONTAINS xxx VALUES INPUT FILE1 'infilename1' : SUM OF VALUES = xxxxxx INPUT FILE2 'infilename2' : SUM OF VALUES = xxxxxx OUTPUT FILE 'outfilename' : CONTAINS xxx VALUES OUTPUT FILE 'outfilename' : SUM OF VALUES = xxxxxx ... DESIGN CONSTRAINTS: (1) Design and implement the following functions. // --------------------------------------------------------------- // Open specified input file. // --------------------------------------------------------------- void OpenInputFile(ifstream & inF, char inFileName[]); // --------------------------------------------------------------- // Return the number of values in input file (already open). // NOTE: The function must first REWIND the file before starting. // --------------------------------------------------------------- int InFileValueCount (istream &inF); // --------------------------------------------------------------- // Read a value from an OPEN input file; set success to FALSE if // value can not be read. // --------------------------------------------------------------- void ReadValue (istream &inF, int & num, bool & success); // --------------------------------------------------------------- // Write a value to output stream, eight(8) values per line. // Separate values by ONE SPACE. // --------------------------------------------------------------- void WriteValue (ostream & outF, int num); // --------------------------------------------------------------- // Return the sum of values in input file (already open). // NOTE: The function must first REWIND the file before starting. // --------------------------------------------------------------- int Sum (istream &inF); (2) DO NOT USE ARRAYS. EXAMPLES: Input file1: 1 2 3 6 10 13 Input file2: 1 4 5 6 10 12 14 Output file: 1 1 2 3 4 5 6 6 10 10 12 13 14 Input file1: 1 2 3 Input file2: Output file: 1 2 3 Input file1: Input file2: 1 2 3 Output file: 1 2 3 Input file1: 5 7 8 Input file2: Output file: 5 7 8 Input file1: (empty) Input file2: (empty) Output file: (empty) Input file1: missing OR Input file1: 2 3 Input file2: 1 2 3 Input file2: missing Output file: not created. Output file: not created. Message: FATAL ERROR: Unable to OPEN file 'file1' (file2)
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 << "FATAL ERROR: unable to open file.";
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.