1030prob2atxt 1030prob2btxt Problem 2 Multiple Files 15 poin

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.

Solution

#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;
   }

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 pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site