SOURCE FILE: sumDoer.cpp
/* This is a program that reads 7 numbers from a file into an array,
calculates the sum of the items in the array, and then
writes the sum to the screen. */
#include <iostream>
#include <fstream>
using namespace std ;
const int SIZE = 7;
const int STRING_MAX = 256 ;
int main (void)
{
double data[SIZE] ;
char fileName[STRING_MAX] ;
double sum ;
int index ;
cout << "Please type the name of the 'from' file: " ;
cin >> fileName ;
ifstream fin ;
fin.open(fileName) ;
if (fin.fail())
{
cout << "FAILED TO OPEN INPUT FILE: " << fileName << endl ;
exit(1) ;
}
for (index=0; index<SIZE; index++ )
{
fin >> data[index] ;
}
fin.close() ;
sum = 0 ; //important starter value
for (index=0; index<SIZE; index++ )
{
sum = sum + data[index] ;
}
cout.setf(ios::fixed) ;
cout.setf(ios::showpoint) ;
cout.precision(2) ;
cout << "\nThe sum of the array values is: " << sum << endl << endl ;
return 0;
}