home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / strstr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  1.0 KB  |  35 lines

  1. /* STRSTR.CPP: This program takes the average of numbers entered at
  2.                              the command line.  Since command line arguments are
  3.                              delivered to any C program in char* form via the argv[]
  4.                              array, ASCII-to-double conversion must be done before
  5.                              the computation can be performed. This is efficiently
  6.                              done using a class of istrstream type as show in this
  7.                              program.
  8. */
  9.  
  10. #include <strstream.h>
  11. #include <string.h>
  12.  
  13. //*******************************************************************
  14. main( int argc, char *argv[] )
  15. {
  16.     unsigned u;
  17.     double total = 0, average = 0, d = 0;
  18.  
  19.     if ( argc == 1 ) {     // Not enough command line parameters!
  20.         cout << endl << "Usage <filename> float float ...." << endl;
  21.         return 1;
  22.   }
  23.  
  24.   for ( u = 1; u < argc; ++u ) {
  25.     istrstream istr( argv[u], strlen(argv[u]) );
  26.         istr >> d;
  27.     total += d;
  28.   }
  29.  
  30.   average = total / (argc - 1);
  31.   cout.setf( ios::fixed, ios::floatfield );
  32.   cout << endl << "AVERAGE = " << average << endl;
  33.   return 0;
  34. }// end of main()
  35.