/* input file failure example file: 3ex3.cpp FALL 2001 ___________________________________ Jacob Y. Kazakia jyk0 May 2, 2001 Programming example 3 of unit #3 Recitation Instructor: J.Y.Kazakia Recitation Section 01 ___________________________________ Purpose: This program reads a sequence of numbers which are written in a file called 3ex3data.txt The file looks like this: 1.00345 4.567 1.89e-03 2.345 1.78 2.897e-6 123.678 34 We assume that the user does NOT know, ahead of time, how many numbers are listed. The task of the program is to output the numbers to the screen and calculate the sum. Algorithm: A stream named ex3input is established. A while loop provides repeated reading of numbers. The condition of the loop is set to while ( ! ex3input.eof() ) When the stream 3ex3input reaches the END OF FILE then ex3input.eof() evaluates to TRUE. As long as we are not at the end it evaluates to FALSE and consequently the prefixed ! is needed. */ #include // header needed for file input output void main() { ifstream ex3input ( "3ex3data.txt", ios :: in); float number ; float sum = 0; ex3input >> number; cout << "\n\n This is the list of numbers read \n\n"; while ( ! ex3input.eof() ) // read the numbers and add them up { cout << number << endl; sum = sum + number; ex3input >> number; } cout << "\n\n End of file reached. The sum is : " ; cout << sum; cout << " \n\n Enter e(exit) to exit ..."; char hold; cin >> hold; } /* THE OUTPUT : This is the list of numbers read 1.00345 4.567 0.00189 2.345 1.78 2.897e-006 123.678 34 End of file reached. The sum is : 167.375 Enter e(exit) to exit ... */