/****************************************************************/ /* Purpose: Trivial example illustrating stdin and eof */ /* (also illustrates use of char for ints) */ /* Name: M. Overstreet */ /* Date: 25 Sept. 2003 */ /* Description: Reads and prints numbers until eof */ /****************************************************************/ /****************************************************************/ /* Listing Contents: */ /* Reuse instructions */ /* Compilation instructions */ /* Source code */ /****************************************************************/ /****************************************************************/ /* Reuse Instructions */ /* Program is so short, reuse opportunities are limited -- */ /* unless the student is really having trouble with stdin */ /* and eof. */ /****************************************************************/ /****************************************************************/ /* Compilation Instructions */ /* Compiled under Solaris with */ /* g++ -o eof eof.cpp */ /****************************************************************/ using namespace std; #include /****************************************************************/ /* Precondition: Input must be valid numerics. */ /* Input must be pairs of numbers */ /* Postcondition: Input values are printed. */ /* Sum and count of number of values read is */ /* printed. */ /****************************************************************/ int main() { char c1, c2; /* int values read */ char sum = 0; /* used to sum elements */ int ct = 0; /* count of number of pairs read */ int tmp; /* Read number pairs till end of file, summing and counting */ cin >> tmp; while ( cin ) { c1 = tmp; sum += c1; ct++; cin >> tmp; c2 = tmp; sum += c2; cout << "c1: " << (int)c1 << ", c2: " << (int)c2 << endl; cout << "sum: " << sum << endl; cout << "sum: " << (int)sum << endl; cin >> tmp; } /* Report results of run */ cout << ct << " pairs of numbers read." << endl << "sum of values: " << (int)sum << endl; }