Print in a Box

I'm having some trouble understanding how I'm supposed to go about coding this specific program to take user input and print the sequence centered in a box made from plus signs at the corners, dashes for the ceiling and floor, and | for the walls. Below is the task assigned from my book and what I have for a code so far.

Write a program that reads a sequence of words and then prints them in a box, with each word centered, like this:
+----------+
| Hello |
| C++ |
|programmer|
+----------+

Cay S. Horstmann. Big C++: Late Objects, 3rd Edition (Page EX4-8). . Kindle Edition.

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

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    int max_len = 0;
    int word_len = 0;
    string response;

    cout << "Please enter a sequence of words: " << endl;
    cin >> response;
    getline (cin, response);
    for (int i=0; i < response.length(); i++)
    {
        if (response.substr(i,1) == " ")
        {
            if (word_len > max_len)
            {
                max_len = word_len;
                word_len = 0;
            }
        }
        else
        {
            word_len++;
        }
    }
    if (word_len > max_len)
            {
                max_len = word_len;
            }
    cout << "+";
    for (int i = 1; i <= max_len; i++)
    {
        cout << "-";
    }
    cout << "+" << endl;
    cout << max_len << endl;

    return 0;
}
Omit line 15 and put the whole line into response (i.e. your line 16)

Create a stringstream from the string response.

Extract successive strings from the stringstream and push_back into a vector<string> to hold your collection of words.

Find the length of the longest word. (You could combine this with the step above).

For each word in your vector<string> add the appropriate number of blanks at start and end. Note that string(r, ' ') produces r blanks, and strings can be added.

Write the top line, followed by newline '\n'.

For each word in your vector<string> output the '|', the blank-extended word and another '|', followed by newline. (Or you could add the start and end characters at the same time as adding blanks earlier.)

Write the bottom line.
Could you possibly do a mock code of this for visual representation?
Which part don't you understand?

There are many, many ways of doing this problem, though I think it best to get your individual words into a vector<string> (just an expansible array of strings) first. Thereafter, you can choose to add the requisite number of blanks before and after to the strings, or you can simply put them in when doing the output. YOUR choice.

Note that writing, e.g.,
cout << '+' + string( max_len, '-' ) + '+' << endl;
would save all your loops on lines 36-41. Since you use it twice you might want to put the string part in a variable first. This repeat-count version of string can also be used to get the requisite number of blanks on the word lines.

Also note that it will never be possible to perfectly centre both odd and even-length words in the same box.
Last edited on
Topic archived. No new replies allowed.