Another Example

// A file whose name is input from the keyboard is created.
// The total number of integers wanted is input from the keyboard.
// Random integers are created and are placed in the file.
// The file is then opened for input.
// The integers are read in and the number of integers is counted.
// Another function computes the sum and the total number of integers read.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

void createFile(char fileName[ ], int size);
// create a file whose name is given by the char array fileName
// and place random integers into the file.
// The number of integers is given by size.

void total(char fileName[ ], int& sum, int& Count);
// Compute the sum of the numbers in the file fileName
// return the number of integers read.
// We imagine that the total number of integers in that file is unknown.

int main( )
{
    char FileName[20];
    int Size, Sum = 0, Count = 0;

    cout << "Enter the name of the file you want to create: ";
    cin >> FileName;

    cout << "Enter the number of random numbers you want in the file: ";
    cin >> Size;

    createFile(FileName, Size);

    total(FileName, Sum, Count);

    cout << "The sum is: " << Sum << endl;
    cout << "The number of integers read is: " << Count << endl;

    if (Count != 0)
       cout << "The average number is: " << double(Sum)/double(Count) << endl;
    else
       cout << "The average is meaningless since there is no number in the file!"
                << endl;

    return 0;
}

void createFile(char fileName[ ], int size)
{
    ofstream outs;
    int count = 0;

    outs.open(fileName,);
    if (outs.fail())
    {
       cout << "Problem opening the file: " << fileName
                << " to output the random numbers.'\n'";
       exit(1);
    }

    // Should not use a do-loop since the file may be empty!
    while (count < size)
    {
       outs << setw(9) << rand( ) << endl;     // needs to include iomanip
       count++;
    }

    outs.close( );
}

void total(char fileName[ ], int& sum, int& count)
{
    ifstream ins;
    int number;

    ins.open(fileName, ios::in);
    // The ios::in flag make sure that if the file does not exist,
    // it will not be created and the open function will fail.

    if (ins.fail())
    {
       cout << "Problem opening the file: " << fileName
                << " to input the random numbers. '\n'";
       exit(1);
    }

    //ins >> number; cout << number << endl;// Uncomment this line!
    while( !ins.eof())
    {
       ins >> number; cout << number << endl;// Delete this line!
       sum += number;
       count++;
       // ins >> number; cout << number << endl; // Uncomment this line!
    }

    ins.close( );

}

The program, whether modified in the way indicated or not, compiles and runs without compilation or run-time errors.

Suppose when the program is run, the user enters eof.txt as the name of the file to be created and to be read later, and enters 10 as the number of random integers wanted.
Actually the above program has logical errors and will generate the following dialogue on the screen:

Enter the name of the file you want to create: eof.txt
Enter the number of random numbers you want in the file: 10
41
18467
6334
26500
19169
15724
11478
29358
26962
24464
24464
The sum is: 202961
The number of integers read is: 11
The average number is: 18451

The about program erroneously sums the last number twice and over counts the random numbers by one. The average is therefore incorrectly computed.

The key point is that ins.eof( ) does not become false until the program attempts to read beyond the end of the file.

A correct version of the program is obtained by modifying three of the lines as indicated in the comments. When run it generates the following dialogue:

Enter the name of the file you want to create: eof.txt
Enter the number of random numbers you want in the file: 10
41
18467
6334
26500
19169
15724
11478
29358
26962
24464
24464
The sum is: 178497
The number of integers read is: 10
The average number is: 17849.7

The above results look almost identical to those generated by the incorrect program, except in the sum, the number of integers read, and the average number, which now have their correct values.