Source of sstream.cpp


  1: // Filename: SSTREAM.CPP
  2: // Purpose:  Illustrates the "input string stream" class.

  4: /*
  5: Typical Usage:
  6: prompt> sstream 1/2 Scobey,Porter

  8: Corresponding Output:

 10: The 3 command-line parameters are:
 11: sstream
 12: 1/2
 13: Scobey,Porter

 15: The numerator of the fraction is 1 and the denominator is 2.
 16: Hello there, Porter Scobey!

 18: */

 20: #include <iostream>
 21: #include <string>
 22: #include <sstream>
 23: using namespace std;

 25: int main(/* in */ int argc,
 26:          /* in */ char* argv[])
 27: {
 28:     cout << "\nThe " << argc << " command-line parameters are: \n";
 29:     for (int i = 0; i < argc; i++) cout << argv[i] << endl;
 30:     cout << endl;

 32:     string a[3] = { "", argv[1], argv[2] };
 33:     // argv[0] is not used anywhere, so a[0] is set to the null string.

 35:     istringstream iss1(a[1]), iss2(a[2]);

 37:     int numer, denom;
 38:     char ch;
 39:     iss1 >> numer >> ch >> denom;
 40:     cout << "The numerator of the fraction is " << numer
 41:          << " and the denominator is "          << denom << "." << endl;

 43:     string lastName, firstName;
 44:     getline(iss2, lastName, ',');
 45:     iss2 >> firstName;
 46:     cout << "Hello there, " << firstName << " " << lastName << "!" << endl;

 48:     return 0;
 49: }