Answer to Question #55564 in C++ for Amid

Question #55564
Write a main function that performs the following
operations:

a.) Prompts the user to input the names of 3 files: two input files and
an output file.

b.) Reads in the two input files line by line and compares the two
lines.

c.) For each line in the inputs, write to the output file the line number,
a colon (':'), and either "OK" if the lines match or "DIFF" if the lines
are different.

The input files may be of different lengths.

Note that:

- You will NEED to use getline(...) If you use the
>> operator, it will ignore leading whitespace (so your program will likely
incorrectly report " aa" and "aa" as being the same).

For example:

input1:
abc
def
g h i

input2:
abc
DEf
ghi
uub

output:
1:OK
2:DIFF
3:DIFF
4:DIFF
1
Expert's answer
2015-10-16T03:11:16-0400
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
string input;
ifstream inputFile1, inputFile2;
ofstream outputFile;
do
{
cout << "Enter first input file name : ";
getline(cin, input);
inputFile1.open(input);

} while (inputFile1.is_open() == false);


do
{
cout << "Enter second input file name : ";
getline(cin, input);
inputFile2.open(input);

} while (inputFile2.is_open() == false);

do
{
cout << "Enter output file name : ";
getline(cin, input);
outputFile.open(input);

} while (outputFile.is_open() == false);

string line1, line2;
int i = 1;
while (getline(inputFile1, line1) && getline(inputFile2, line2))
{
if (line1.compare(line2) == 0)
{
outputFile << i << ":OK\n";
}
else
{
outputFile << i << ":DIFF\n";
}
i++;
}
while (getline(inputFile1, line1))
{
outputFile << i << ":DIFF\n";
i++;
}
while (getline(inputFile2, line2))
{
outputFile << i << ":DIFF\n";
i++;
}
outputFile.close();
inputFile1.close();
inputFile2.close();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS