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.
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.)
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.