The name_box1 program is the first of three versions exploring strings and their operations. The programs and the problems they solve are similar, but each demonstrates a different string implementation or different data input. The first version demonstrates C-strings reading input from the console.
+-----------------+ |Cranston Q. Snort| +-----------------+
I recommend that software developers learning to solve problems with programs, develop them incrementally. The first increment includes four fundamental tasks: First, outline the program. At a minimum, the outline includes the necessary headers and defines the main function with its return statement. The second task is to define a minimal set of variables. Third, the program performs the first input operation. And lastly, the program echoes the just-read data to the console; this last step may require writing code that is unnecessary for the completed program, and is discarded as development progresses.
| Attempt 1 | Attempt 2 |
|---|---|
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[100];
cout << "Please enter your name: ";
cin >> name;
cout << name;
return 0;
} |
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[100];
cout << "Please enter your name: ";
cin.getline(name, 100);
cout << name;
return 0;
} |
Cranston Q. Snort, the program displays Cranston on the console. The review, problem statement, and algorithm all hinted at this problem: the extractor, >>, doesn't read past whitespace characters. However, "real-world" problems are generally less helpful. The review also suggested the correct input function: getline. Attempt 2 demonstrates the correct output, completing the first two incremental attempts.
| Attempt 3 | Attempt 4 |
|---|---|
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[100];
cout << "Please enter your name: ";
cin.getline(name, 100);
for (size_t i = 0; i < strlen(name); i++)
cout << "-";
cout << endl << name;
return 0;
} |
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[100];
cout << "Please enter your name: ";
cin.getline(name, 100);
cout << "+";
for (size_t i = 0; i < strlen(name); i++)
cout << "-";
cout << "+" << endl;
cout << "|" << name << "|" <<;
return 0;
} |
----------------- Cranston Q. Snort |
+-----------------+ |Cranston Q. Snort| |
To complete algorithmic step 3, attempt 4 prints the corners of the box, one at each end of the top line. It moves printing the newline up from the name to the right-hand corner. To maintain the character alignment, the attempt also completes steps 4, 5, and 6, printing the name surrounded by the left and right sides of the box. The output, shown below the code, validates the partial program.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[100]; // step 1
cout << "Please enter your name: ";
cin.getline(name, 100); // step 2
cout << "+"; // step 3.a
for (size_t i = 0; i < strlen(name); i++) // step 3.b
cout << "-";
cout << "+" << endl; // step 3.c
cout << "|" << name << "|" << endl; // steps 4, 5, and 6
cout << "+"; // step 7.a
for (size_t i = 0; i < strlen(name); i++) // step 7.b
cout << "-";
cout << "+" << endl; // step 7.c
return 0;
}