C++ - loop and conditions example ================================= Public domain ******************************************************************************** // ask for a person's name, and generate a framed greeting #include #include using namespace std; int main() { // ask for the person's name cout << "Please enter your first name: "; string name; cin >> name; // build the message that we intend to write const string greeting = "Hello, " + name + "!"; // the number of blanks surrounding the greeting const int pad = 6; // the number of rows and columns to write const int rows = pad * 2 + 3; const string::size_type cols = greeting.size() + pad * 2 + 2; // write a blank line to separate the output from the input cout << endl; for (int i=0; i < rows; ++i) { for (string::size_type j=0; j < cols; ++j) { if (j == 0 || j == cols - 1 || i == 0 || i == rows - 1) cout << "*"; else if (i == pad + 1 && j > pad && j < cols - pad - 1) { cout << greeting; j += greeting.size() - 1; } else cout << " "; } cout << endl; } return 0; } Output : Please enter your first name: Pejman **************************** * * * * * * * * * * * * * Hello, Pejman! * * * * * * * * * * * * * **************************** ******************************************************************************** _BY: Pejman Moghadam_ _TAG: cpp, string-size-type_ _DATE: 2011-09-18 00:00:51_