8.2.3. name_box.cpp , Version 1
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:
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
Input a string of unknown length that will contain spaces
Determine how many dashes to draw for the top and bottom of the box
Print the corners of the box
Print the sides of the box
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).
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.
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.
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.
Problem (4): Prints the top and bottom left-hand corners but leaves the cursor on the current line.
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.
Problem (4): prints the top and bottom right-hand corners and moves the cursor to the next line.
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.
Back |
Chapter TOC |
Next