Wednesday, June 29, 2011

Testing

#include <iostream>
#include <string>

using namespace std;

int main()
{
 cout << "Please enter your first name: ";

 string name;
 cin >> name;

 const string greeting = "Hello, " + name + "!";

 cout << "Enter the vertical and horizontal padding: ";
 int padTopBottom;
 int padLeftRight;
 cin >> padTopBottom >> padLeftRight;

 const int rows = padTopBottom * 2 + 3;
 const string::size_type cols = greeting.size() + padLeftRight * 2 + 2;

 cout << endl;

 for (int r = 0; r != rows; ++r)
 {
  string::size_type c = 0;

  while (c != cols)
  {
   if (r == padTopBottom + 1 && c == padLeftRight + 1)
   {
    cout << greeting;
    c += greeting.size();
   }
   else
   {
    if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
    {
     cout << "*";
     ++c;
    }
    else
    {

     // The line where the greeting goes.  Should only be here twice
     if ( r == padTopBottom + 1 )
     {
      string space(padLeftRight, ' ');
      cout << space;
      c += padLeftRight;
     }
     else
     {
      string space(cols - 2, ' ');
      cout << space;
      c += cols - 2;
     }
    }
   }
  }

  cout << endl;
 }

 return 0;
}

No comments: