EOF Sum


Program Listing

EOFSum.cpp

/************************************************************************
*                                                                       *
*   Program:    EOFSum                                                  *
*                                                                       *
*   Abstract:   This program will add up a list of input values.        *
*               It uses a loop controlled by EOF.                       *
*                                                                       *
************************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {
    int Total = 0;
    cout << "Enter an integer (type CTRL-D to quit):  " << flush;
                                // Prompt
    int Number;

//  Add to total, until end of file

    while (cin >> Number) {     // Read & test
        Total += Number;        // Action
        cout << "Enter an integer (type CTRL-D to quit):  " << flush;
                                // Prompt
    }

//  Print the total

    cout << endl << endl << "The total is " << Total << endl;
    return 0;
}

Sample Run #1

Enter an integer (type CTRL-D to quit):  20
Enter an integer (type CTRL-D to quit):  30
Enter an integer (type CTRL-D to quit):  40
Enter an integer (type CTRL-D to quit):  

The total is 90

Sample Run #2

Enter an integer (type CTRL-D to quit):  

The total is 0



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:33 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/EOFSum.html

Return to Coms 261 home page