Source of cstr4.cpp


  1: // Filename: CSTR4.CPP
  2: // Purpose:  Illustrates explicit delimiter with
  3: //           cin.getline and C-strings.

  5: #include <iostream>
  6: #include <cstring>
  7: using namespace std;

  9: #include "PAUSE.H"

 11: int main()
 12: {
 13:     cout << endl;
 14:     cout << "This program allows the user to choose "
 15:          << "both the maximum number of characters "         << endl
 16:          << "to read per string for each of two strings, "
 17:          << "as well as the delimiter character "            << endl
 18:          << "which terminates the reading of each string. "  << endl;
 19:     cout << endl;

 21:     const int MAX_STR_SIZE = 10;
 22:     typedef char String10[MAX_STR_SIZE + 1];
 23:     String10 s1, s2;


 26:     int numChars;
 27:     cout << "Enter the number (<= " << MAX_STR_SIZE
 28:          << ") of characters to read: ";
 29:     cin >> numChars;
 30:     cout << endl;  cin.ignore(80, '\n');

 32:     char delimiter;
 33:     cout << "Enter the delimiter character: ";
 34:     cin.get(delimiter);
 35:     if (delimiter != '\n')
 36:         cin.ignore(80, '\n');
 37:     cout << endl;

 39:     cout << "Enter some text: " << endl;
 40:     cin.getline(s1, numChars+1, delimiter);
 41:     cin.getline(s2, numChars+1, delimiter);

 43:     cout << endl;
 44:     cout << "Here are the two strings as read "
 45:          << "(\"<<\" points to the end of each):"   << endl;
 46:     cout << s1 << "<<" << endl;
 47:     cout << s2 << "<<" << endl;

 49:     cout << endl;

 51:     return 0;
 52: }