All pastes #1352523 Raw Edit

[phear]crippler

public cpp v1 · immutable
#1352523 ·published 2009-03-04 06:46 UTC
rendered paste body
/*AUTHOR: David ChapmanFILE: assignment5.cppPURPOSE: Take input from a file of numbers of type double and output 		 the average of the numbers in the file to the screen.*/#include <iostream>#include <fstream>#include <iomanip> using namespace std;ofstream outputfile("output.txt");const int MAX_FILE_NAME = 35; void open_input(ifstream& input, char name[]);          void average(ifstream& input, double& av_rage);void output(const char name[], double av_rage, ostream& os = cout); int main()     {  	char again;                        	char file_name[MAX_FILE_NAME + 1]; 	ifstream input_numbers;            	double av_rage;                     	cout << "This program can average the numbers in a file\n" << endl;	system("pause");	do 	{  	    system("cls");                         		open_input(input_numbers, file_name);    		average(input_numbers, av_rage);   	    input_numbers.close();                    	output(file_name, av_rage);             	    output(file_name, av_rage, outputfile);   		cout << "\nDo you want to process another file (Y/N)? ";		cin >> again;		cin.ignore(256, '\n');    } while ( again == 'y' || again == 'Y'); 	cout << "\nEnd of Program!" << endl;	outputfile.close();return 0; }  void open_input(ifstream& input, char name[]) {  	int count = 0;                do 	{  		  count++;		if (count != 1)  		 {  cout << "\n\aInvalid file name or file does not exist. Please try again." 			      << endl;		}	    cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME             << " characters please)\n:> ";        cin.get(name, MAX_FILE_NAME + 1);        cin.ignore(256, '\n');           		input.clear();                           input.open(name, ios_base::in);    } while (input.fail() );   }void average(ifstream& input, double& av_rage)  {    double nxt, sum = 0;  int count = 0;  while(input >> nxt)  {	  sum = sum + nxt;	  count++;  }av_rage = sum / count;   } void output(const char name[], double av_rage, ostream& os) {  	os << "\n\nInput File Name        : " << name << endl;	os << "Average = " << setw(8) << av_rage << endl; } /*Enter the input file name (maximum of 35 characters please):> class1.txtInput File Name        : class1.txtAverage =    78.53Do you want to process another file (Y/N)?*/