# Exercise 2-2

Change the framing program (Exercise 2-0) so that it uses a different amount of space to separate the sides from the greeting than it uses to separate the top and bottom borders from the greeting.

# Solution

The original framing program in Exercise 2-0 “hard-codes” the const int pad which forces both vertical and horizontal paddings to be identical. The question asks for a capability to assign the vertical padding and horizontal padding separately.

To do this I am going to adjust the original framing program taking into account of the followings:

• Ask the user to provide the vertical padding, and store it as an int variable inPadY.
• Ask the user to provide the horizontal padding, and store it as an int variable inPadX.
• Replace all the (old) pad references associating with rows with padY (e.g. the constant rows and invariant r). Because the number of rows depends on vertical padding.
• Replace all the (old) pad references associating with columns with padX (e.g. the constant cols and invariant c). Because the number of columns depends on horizontal padding.

The modified program looks like this:

#include <iostream>
#include <string>

// say what standard-library names we use
using std::cin;
using std::endl;
using std::cout;
using std::string;

int main()
{
// ask for the person's name

string name;
cin >> name;

// build the message that we intend to write
const string greeting = "Hello, " + name + "!";

// build the constant padding parameters

// the number of rows to write
const int rows = ( padY * 2 ) + 3;

// the number of columns to write
const string::size_type cols = greeting.size() + ( padX * 2 ) + 2;

// write a blank line to separate the output from the input
cout << endl;

// write 'rows' rows of output
// invariant: we have written r rows so far
for ( int r = 0; r != rows ; ++r )
{
string::size_type c = 0;

// invariant: we have written c characters so far in the current row
while ( c != cols)
{
// is it time to write the greeting?
if ( ( r == padY+ 1 ) && ( c == padX + 1 ) )
{
cout << greeting;
c += greeting.size();
}
else
{
// are we on the border?
if (
( r == 0 )         // top row
|| ( r == rows - 1 )  // or bottom row
|| ( c == 0 )         // or left-most column
|| ( c == cols - 1 )  // or right-most column
)
{
cout <<  "*";
}
else
{
cout << " ";
}
++c;
}
}
cout << endl;
}
return 0;
}


# Result

Please enter your first name: Johnny

************************************
*                                  *
*          Hello, Johnny!          *
*                                  *
************************************


Please enter your first name: Johnny

************************************
*                                  *
*          Hello, Johnny!          *
*                                  *
************************************


Please enter your first name: Johnny

**************************
*                        *
*                        *
*                        *
*                        *
*                        *
*     Hello, Johnny!     *
*                        *
*                        *
*                        *
*                        *
*                        *
**************************


So far, so good!

# Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000