Source of prnteven.cpp


  1: // Filename: PRNTEVEN.CPP
  2: // Purpose:  Prints out the even digits in a positive integer.

  4: #include <iostream>
  5: #include <iomanip>

  7: void DisplayIDInfo(int);
  8: void DescribeProgram();
  9: void DisplayMainMenu(int);
 10: void GetMenuChoice(int, int&);
 11: void Pause(int);
 12: void PrintEvenDigits(int);


 15: int main()
 16: {
 17:     const INDENT_LEVEL = 20;
 18:     int menuChoice;
 19:     int n;

 21:     DisplayIDInfo(INDENT_LEVEL/2);

 23:     do
 24:     {
 25:         DisplayMainMenu(INDENT_LEVEL);
 26:         GetMenuChoice(INDENT_LEVEL, menuChoice);

 28:         switch (menuChoice)
 29:         {
 30:             case 1:
 31:                 cout << setw(INDENT_LEVEL) << ""
 32:                      << "Program is now terminating. "
 33:                      << endl;
 34:                 break;
 35:             case 2:
 36:                 DescribeProgram();
 37:                 break;
 38:             case 3:
 39:                 cout << setw(INDENT_LEVEL) << ""
 40:                      << "Enter a positive integer: ";
 41:                 cin >> n;  cin.ignore(80, '\n');  cout  << endl;
 42:                 cout << setw(INDENT_LEVEL) << ""
 43:                      << "Even digits in " << n << " are: ";
 44:                 PrintEvenDigits(n);
 45:                 cout << endl << endl;
 46:                 break;
 47:         }

 49:         if (menuChoice != 1)
 50:             Pause(0);

 52:     } while (menuChoice != 1);

 54:     cout << endl;

 56:     return 0;
 57: }
 58: 
 59: void DisplayIDInfo(int indentLevel)
 60: // Pre:  indentLevel contains a nonnegative integer.
 61: // Post: A blank line, and a line containing the programmer's name
 62: //       have been displayed.
 63: {
 64:     cout << endl;
 65:     cout << setw(indentLevel) << ""
 66:          << "This sample program produced by Your_Name_Goes_Here.";
 67:     cout << endl;
 68: }


 71: void DescribeProgram()
 72: // Pre:  none
 73: // Post: A program description has been displayed.
 74: {
 75:     cout << endl;
 76:     cout << "This program outputs the even digits "
 77:          << "in a positive integer (in the same order)."  << endl;
 78:     cout << endl;
 79: }


 82: void DisplayMainMenu(int indentLevel)
 83: // Pre:  indentLevel contains a nonnegative integer.
 84: // Post: A menu has been displayed, each line indented indentLevel spaces.
 85: {
 86:     cout << endl << endl;
 87:     cout << setw(indentLevel) << "" << "Main Menu ";
 88:     cout << endl << endl;
 89:     cout << setw(indentLevel) << "" << "1. Quit "               << endl;
 90:     cout << setw(indentLevel) << "" << "2. Get information "    << endl;
 91:     cout << setw(indentLevel) << "" << "3. Print even digits "  << endl;
 92:     cout << endl << endl;
 93: }


 96: void GetMenuChoice(int indentLevel, int& menuChoice)
 97: // Pre:  indentLevel contains a nonnegative integer.
 98: // Post: A prompt has been displayed, indented indentLevel spaces,
 99: //       and menuChoice contains an integer entered by the user.
100: {
101:     cout << setw(indentLevel) << "" << "Enter your choice here: ";
102:     cin >> menuChoice;  cin.ignore(80, '\n');
103:     cout << endl << endl;
104: }


107: void Pause(int indentLevel)
108: // Pre:  The standard input stream cin is empty, and indentLevel 
109: //       contains a nonnegative integer.
110: // Post: A prompt has been displayed, indented indentLevel spaces,
111: //       and the user has pressed <RETURN>.
112: {
113:     char newlineChar;
114:     cout << setw(indentLevel) << "Press <RETURN> to continue ... ";
115:     cin.get(newlineChar);
116:     cout << endl;
117: }
118: 
119: void PrintEvenDigits(/* in */ int n)
120: // Pre:  n contains a positive integer.
121: // Post: All even digits in n have been output in the order in
122: //       which they appeared in n, with no spaces between them.
123: {
124:     if (n < 10)
125:     {
126:         if (n % 2  ==  0)
127:             cout << n;
128:     }
129:     else
130:     {
131:         int t = n % 10;
132:         PrintEvenDigits(n / 10);
133:         if (t % 2  ==  0)
134:             cout << t;
135:     }
136: }