Exercise 2-1
Change the framing program (Exercise 2-0) so that it writes its greeting with no separation from the frame.
Solution
The sort of output that we are after looks like this:
</p> <hr /> <p><em>Hello, Johnny!</em></p> <hr /> <p>
i.e. We can just simply re-use the same program that we wrote in Exercise 2-0, except this time we change the padding of 1 (space character) to 0 (space characters). In other word, no padding round the greeting message.
For completeness sake, I have copied and pasted the code from Exercise 2-0 here. Note that this time I change the const int pad value to 0. (See line 23 below!)
</p> <h1>include <iostream></h1> <h1>include <string></h1> <p>// say what standard-library names we use using std::cin; using std::endl; using std::cout; using std::string;</p> <p>int main() { // ask for the person's name cout << "Please enter your first name: ";</p> <pre><code>// read the name string name; cin &gt;&gt; name; // build the message that we intend to write const string greeting = &quot;Hello, &quot; + name + &quot;!&quot;; // the number of blanks surrounding the greeting const int pad = 0; // no padding this time // 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 &lt;&lt; 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 == pad + 1 ) &amp;&amp; ( c == pad + 1 ) ) { cout &lt;&lt; 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 &lt;&lt; &quot;*&quot;; } else { cout &lt;&lt; &quot; &quot;; } ++c; } } cout &lt;&lt; endl; } return 0; </code></pre> <p>}</p> <p>
Result
The program compiles successfully and produce the following output in the standard console output window.
Please enter your first name: Johnny</p> <hr /> <p><em>Hello, Johnny!</em></p> <hr /> <p>Process returned 0 (0x0) execution time : 2.905 s Press any key to continue.