8.2.3. name_box.cpp, Version 1

Time: 00:03:37 | Download: Large Small | Streaming

The following program is the first of three versions we use to explore strings and C-strings. The three programs are similar, but the problems they solve are modified to demonstrate different concepts. The first version uses C-strings to solve the following problem:

+-----------------+
|Cranston Q. Snort|
+-----------------+
The first name-box problem. The basic problem is to allow users to enter their name and then draw a box around the name with ASCII characters on the console. We add a further constraint that there may not be spaces between the sides of the box and the first and last characters of the name. The constraint makes one of the subsequent versions of the program more "interesting." Recalling from an earlier discussion that the extractor operator, >>, does not read past spaces, and realizing that having at least a first and a last name, separated by a space, is a common practice in most cultures, we need to use a different way of reading the user's name. We can make a list of sub-problems that the program must solve from this problem description:
  1. Define a C-string (a character array) long enough to store a full name of unknown length; it is not necessary to initialize the C-string to empty
  2. Input a string of unknown length that will contain spaces
  3. Determine how many dashes to draw for the top and bottom of the box
  4. Print the corners of the box
  5. Print the sides of the box
  6. Print the user's name without spaces at the beginning or end (i.e., no space between the box and name)
#include <iostream>
#include <cstring>					// (a)
using namespace std;

int main()
{
	char	name[100];				// (b)

	cout << "Please enter your name: ";
	cin.getline(name, 100);				// (c)

	cout << "+";					// (d)
	for (size_t i = 0; i < strlen(name); i++)	// (e)
		cout << "-";
	cout << "+" << endl;				// (f)

	cout << "|" << name << "|\n";			// (g)

	cout << "+";					// (d)
	for (size_t i = 0; i < strlen(name); i++)	// (e)
		cout << "-";
	cout << "+" << endl;				// (g)

	return 0;
}
name_box1.cpp. Each program statement solves one of the stated problems or satisfies some routine program "housekeeping" (e.g., the include directives or the using and return statements).
  1. Allows the program to use the C-string functions. Some systems may not require this header, but your programs will be more portable if you include it.
  2. Problem (1): defines a character array of 100 elements (i.e., a C-string with a capacity of 99 characters), which will likely waste space but should be sufficient for most names.
  3. Problem (2): Reads a string, including spaces, until it detects the new-line character. The getline function adds the null termination character at the end of the input string.
  4. Problem (4): Prints the top and bottom left-hand corners but leaves the cursor on the current line.
  5. Problem (3): The strlen function returns the number of characters, including spaces, in the user's name. The for-loop prints one dash for each letter in the user's name. The cursor remains on the current line following the last dash.
  6. Problem (4): prints the top and bottom right-hand corners and moves the cursor to the next line.
  7. Problems (5 and 6): Print the sides of the box with the name between them. While we can't use >> to read a C-string containing spaces, << works without error. This version of the program prints the name just as the user enters it, so there isn't a problem with spaces at the beginning or end of the name.