// ID: Joseph Wyatt, CIS 163, 10-04-02
// PURPOSE: Demonstrate files, loops, summing
/* DESIGN:
open file
read 2 integers from file
while not at end of file
add the integers giving the sum
output sum
add the sum to the running total
read 2 integers from file
end while
output the running total
*/ close file
//-------------------------------------------
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
const string LINES = "+=========================+\n";
ifstream dataFile; // input data - integers
int num1, num2, sum; // for sum equation
int total = 0; // must init - accumulator
// open file & read first 2 integers
dataFile.open("data.dat");
dataFile >> num1 >> num2;
// print header (also used as footer)
cout << LINES << endl;
// while there's data, add nums, add to total, output, read 2 more nums
while (dataFile != 0)
{
sum = num1 + num2;
total = total + sum;
cout << left << setw(12) << " The sum of "
<< right << setw(4) << num1 << " & "
<< setw(4) << num2 << " = " << setw(4)
<< sum << endl << endl;
// read next data
dataFile >> num1 >> num2;
}
// output grand total, close file and end program
cout << " The grand total is " << total << endl;
cout << endl << LINES << endl;
dataFile.close();
return 0;
}