Source of cstr2.cpp


  1: // Filename: CSTR2.CPP
  2: // Purpose:  Illustrates C-style strings: input with cin, comparison,
  3: //           and output with cout.

  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 illustrates input with cin, "
 15:          << "comparison, and more output with "         << endl
 16:          << "cout, of C-strings.  Study the source "
 17:          << "code while running it.  In response to "   << endl
 18:          << "each prompt, enter either two strings, "
 19:          << "each containing 10 or fewer characters, "  << endl
 20:          << "or an end-of-line to quit. "               << endl;
 21:     cout << endl;
 22:     Pause(0);  cout << endl;

 24:     typedef char String10[11];
 25:     String10 s1, s2;

 27:     cout << "Enter the two strings below: " << endl;
 28:     cin  >> s1 >> s2;  cin.ignore(80, '\n');

 30:     while (cin)
 31:     {
 32:         cout << "The two strings entered and read "
 33:              << "are on the next two lines:"        << endl;
 34:         cout << s1 << "<<" << endl;
 35:         cout << s2 << "<<" << endl;
 36:         Pause(0);  cout << endl;

 38:         // The following three if-statements, written separately here to
 39:         // show each of the three possibilities in full, would normally
 40:         // be written as a nested if with an else at the end.
 41:         if (strcmp(s1, s2) == 0)
 42:             cout << "Those two strings were the same.";
 43:         if (strcmp(s1, s2) < 0)
 44:             cout << "The 1st string precedes the "
 45:                  << "2nd string, alphabetically.";
 46:         if (strcmp(s1, s2) > 0)
 47:             cout << "The 2nd string precedes the "
 48:                     "1st string, alphabetically.";
 49:         cout << endl << endl;
 50:         Pause(0);  cout << endl;

 52:         cout << "Enter the two strings below: " << endl;
 53:         cin  >> s1 >> s2;  cin.ignore(80, '\n');
 54:     }

 56:     cout << endl;

 58:     return 0;
 59: }