Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1030prob2a.txt 1030prob2b.txt Problem 2: Multiple Files (15 points) Write a prog

ID: 3936344 • Letter: 1

Question

1030prob2a.txt

1030prob2b.txt

Problem 2: Multiple Files (15 points) Write a program to read numbers from two separate files. After reading each number, your program needs to sum the two read numbers and write the sum in a third file. Use the file "1030Prob2a.txt" and “1030Prob2b.txt" for this program. Note that the number of numbers in the two files may not be the same and you don't know which file is shorter. You'll have to add zeros when you run out of number in the shorter file i.e. just write the value from the longer file.

Explanation / Answer

#include<iostream>
#include<Fstream>
using namespace std;

int main()
{
   ifstream inFile1;
   ifstream inFile2;

   ofstream outputFile;

   double num1, num2;

   inFile1.open("1030prob2a.txt");
   inFile2.open("1030prob2b.txt");

outputFile.open("outfile_nov7.txt");

   if (inFile1.fail() || inFile2.fail() || outputFile.fail())
{
cout << " Error unable to open checkIn.dat file " << endl;
exit(1);
}

   while(!inFile1.eof() || !inFile2.eof())
   {
       if( inFile1.eof() )
           num1 = 0;
       else
       inFile1>>num1;
       if( inFile2.eof() )
           num2 = 0;
       else
       inFile2>>num2;

       outputFile<<num1<<"+"<<num2<<"= "<<num1+num2<<endl;
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote