Like the first version of the name_box program, the second version continues to display a user's name inside a box drawn with ASCII characters. But for the second version, we change how users enter their names: the second version takes the user's name from the command line. Although the second program's output is the same as the first, taking the input from the command line requires us to solve some additional, challenging sub-problems.
name_box2▢Cranston▢Q.▢Snort
Name
Spaces
argc
Cranston
0
2
Cranston▢Snort
1
3
Cranston▢Q.▢Snort
2
4
(a)
(b)
Entering the user's name on the command line.
A command line is a C-string that names a program, the command, that a user wants the system to run, name_box2 in this example. Some programs allow users to add a space-separated list of arguments following the command name - the user's name in this example (red boxes represent spaces). Our program is not responsible for processing the initial command line. Instead, part of the operating system called the command line processor reads the command line and parses or separates it into its elements, discarding the spaces as it does so. The command line processor places each element into the argv array, which is one of the parameters of the main function that starts every C++ program. So, our program begins with the program's and the user's names in argv:
argv[0] = name_box2
argv[1] = Cranston
argv[2] = Q.
argv[3] = Snort
Armed with the above information, we're now ready to design an algorithm to print the user's name on the console. As described in part (a), we know that the command line processor provides the command line arguments to our program as a parameter named argv. It also passes another parameter named argc, the number of command-line arguments. The command-line processor includes the name of the command or program in argv and counts it in argc, but we don't include the command name or the space between it and the first argument in the Table (b). Most of our effort goes to printing the sides of the box and all the users' names.
In program 1, we could print the sides of the box and the name with a single statement, but in this version, the user's full name consists of multiple strings saved in an array. So, we'll use a for-loop controlled by argc.
In this version of the problem, the constraint of not allowing spaces before or after the name becomes significant. The constraint, coupled with the loop used to print all the user's names, is another example of the fence post problem. Our program will use one of the fence post problem solutions.
To draw the dashes for the top and bottom of the box, we need to know the total number of characters in the user's name. Users may enter any number of names, and the command line processor will discard the spaces between them. So, we'll need to adjust the number of dashes for each removed space. We need to generalize the three examples in Table (b), which shows the relationship between the number of names and the number of spaces the processor removed.
The following program implements the solution outlined here. The second program follows the above solution but makes a few changes.