logo CodeStepByStep logo

printBox

Language/Type: C++ basics streams file input
Related Links:
Author: Marty Stepp (on 2016/06/16)

Write a function named printBox that accepts two parameters: a string holding a file name, and an integer for a width. Your function should open that file and reads its contents as a sequence of lines, and display the lines to the console with a 'box' border of # characters around them on all four sides. The second parameter to your function indicates the total width of the box including its border. You must also convert each line to "title case" by capitalizing the first letter of the line and lowercasing all subsequent letters. For example, suppose the file poem.txt contains the following text:

roses ARE red
VIOLETS Are blUE

All my BASE
ARE belong To YOU

Then the following calls would produce the following console output. If any lines in the file are too long to fit into the box, truncate them.

printBox("poem.txt", 19); printBox("poem.txt", 30); printBox("poem.txt", 7);
###################
#Roses are red    #
#Violets are blue #
#                 #
#All my base      #
#Are belong to you#
###################
##############################
#Roses are red               #
#Violets are blue            #
#                            #
#All my base                 #
#Are belong to you           #
##############################
#######
#Roses#
#Viole#
#     #
#All m#
#Are b#
#######

If the width value passed is less than 2, throw an int exception. Notice that the file might contain blank lines. If the input file does not exist or is not readable, your function should print no output. Your solution should read the file only once, not make multiple passes over the file data.

Type your C++ solution code here:


This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.


Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.