Write a main function that performs the following operations: a.) Prompts the us
ID: 673880 • Letter: W
Question
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 the facilities presented in Chapter 10
(specifically, getline(...)) to complete this assignment. If you use the
>> operator, it will ignore leading whitespace (so your program will likely
incorrectly report " aa" and "aa" as being the same).
- Your program should be case-sensitive, so you do NOT need to worry
about converting text to lowercase or uppercase.
For example:
input1:
abc
def
g h i
input2:
abc
DEf
ghi
uub
output:
1:OK
2:DIFF
3:DIFF
4:DIFF
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main() {
string in1, in2, out;
cout<<"Enter 1st file name: ";
cin>>in1;
cout<<"Enter 2nd file name: ";
cin>>in2;
cout<<"Enter output file:";
cin>>out;
ofstream outfile;
outfile.open (out);
ifstream infile1, infile2;
infile1.open(in1);
infile2.open(in2);
int i=1;
while (!infile1.eof()) {
string s1,s2;
getline(infile1, s1);
getline(infile2, s2);
if(s1.compare(s2) == 0) {
outfile<<i<<":OK";
}else {
outfile<<i<<":DIFF";
}
i++;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.