/* Filename: readkeyb.cpp Author: Br. David Carlson; Br. Isidore Minerd Date: January 8, 2000 Modified: June 26, 2000; August 22, 2001; July 16, 2009 This program reads floating point numbers from the keyboard and finds and prints their average. The numbers should be entered one at a time, as prompted. Use CTRL z to end data input when using Visual C++ on a PC. You may need to press ENTER a couple of times. However, this must be changed to using CTRL d when using g++ under Linux, as CTRL d is the Unix/Linux end of file. This will also fail when an invalid format of data is entered. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET Microsoft Visual Studio 2008 g++ under Linux */ #include using namespace std; int main(void) { float Num, Total; int Count; Count = 0; Total = 0.0; // Warning: Change this to CTRL d for Linux: cout << "Enter a floating point number (or CTRL z to end): " << endl; cin >> Num; while (! cin.fail()) { Total = Total + Num; Count++; // Warning: Change this to CTRL d for Linux: cout << "Enter a floating point number (or CTRL z to end): " << endl; cin >> Num; } if (Count > 0) cout << "Average is " << Total / Count << endl; else cout << "No data given" << endl; return 0; }