Source of draw_boxes.cpp


  1: //draw_boxes.cpp
  2: //Prompts the user to enter a punctuation character and
  3: //then uses that character to draw a 4 by 4 "empty" box.
  4: //The box is preceded and followed by a blank line, and centered
  5: //between the left/right margins of a typical 80-column display.
  6: //If the user does not enter a punctuation character, the program
  7: //displays an error message, does not attempt to draw a box,
  8: //and asks the user to try again. The program terminates when
  9: //the user enters the end-of-file character in response to the
 10: //request to enter a character for the box border.


 13: #include <iostream>
 14: #include <iomanip>
 15: #include <cctype>
 16: using namespace std;

 18: void DescribeProgram();
 19: void GetCharFromUser(char& ch, bool& charOK, bool& timeToQuit);
 20: void DrawBox(char borderChar);


 23: int main()
 24: {
 25:     DescribeProgram();

 27:     char borderChar;
 28:     bool borderCharOK;
 29:     bool timeToQuit;

 31:     GetCharFromUser(borderChar, borderCharOK, timeToQuit);
 32:     while (!timeToQuit)
 33:     {
 34:         if (borderCharOK)
 35:             DrawBox(borderChar);
 36:         else
 37:             cout << "Error: Character input was "
 38:                 "not punctuation. Try again ...\n";
 39:         GetCharFromUser(borderChar, borderCharOK, timeToQuit);
 40:     }
 41:     cout << endl;
 42: }

 44: void DescribeProgram()
 45: //Pre:  The cursor is at the left margin.
 46: //Post: The program description has been displayed,
 47: //      preceded and followed by at least one blank line.
 48: {
 49:     cout << "\nThis program draws a 4-character by 4-character \"empty\" "
 50:         "box. It uses a\ncharacter input by the user, and centers the "
 51:         "box between the left/right\nmargins on a typical 80-column "
 52:         "display.\n\n";
 53: }


 56: void GetCharFromUser(/* out */ char& ch,
 57:                      /* out */ bool& chOK,
 58:                      /* out */ bool& timeToQuit)
 59: //Pre:  None
 60: //Post: ch contains the character entered by the user
 61: //      Value of chOK is
 62: //          - true if ch contains a punctuation character
 63: //          - false otherwise
 64: //      Value of timeToQuit is
 65: //          - true if user has entered the end-of-file character
 66: //          - false otherwise
 67: {
 68:     cout << "\nEnter a punctuation character here "
 69:         "(or end-of-file to quit): ";
 70:     cin >> ch;
 71:     timeToQuit = !cin;
 72:     if (!timeToQuit)
 73:     {
 74:         cout << endl;
 75:         chOK = (ispunct(ch) != 0);
 76:     }
 77: }


 80: void DrawBox(/* in */ char borderChar)
 81: //Pre:  borderChar has been initialized.
 82: //Post: A 4 x 4 "empty" box has been displayed,
 83: //      using the character in borderChar.
 84: {
 85:     cout << endl;
 86:     cout << setw(38) << ""
 87:          << borderChar << borderChar << borderChar << borderChar << endl
 88:          << setw(38) << ""
 89:          << borderChar <<             "  "         << borderChar << endl
 90:          << setw(38) << ""
 91:          << borderChar <<             "  "         << borderChar << endl
 92:          << setw(38) << ""
 93:          << borderChar << borderChar << borderChar << borderChar << endl;
 94:     cout << endl;
 95: }