-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_5_4.cpp
More file actions
61 lines (49 loc) · 1.42 KB
/
2_5_4.cpp
File metadata and controls
61 lines (49 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
// say what standard-library we use
using std::cin; using std::endl; using std::cout; using std::string;
int main()
{
// ask for the person's name
cout << "Please enter your first name: ";
// read the 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
int pad = 1;
// get user input for padding
cout << "How much padding do you want? ";
cin >> pad;
// 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;
// construct blanklines
const string spaces(greeting.size() + pad * 2, ' ');
const string blankline = "*" + spaces + '*';
// 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;
// invatiant: 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 && c == pad + 1) {
cout << greeting;
c += greeting.size();
} else {
// are we on the border?
if (r == 0 || r == rows - 1 || c == 0 || c == cols -1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
}
return 0;
}