|
|
|
|
|
|
|
float angle[100];
float velocity[100]; |
|
|
|
|
|
|
|
|
Up to this point, we haven't used Typedef to give names to array types. We tend to picture an integer or floating point array as a group of individual, separate components. On the other hand, we often visualize a string as a complete unit, almost as if it were a simple data type. Using Typedef to give a name to a string type gives us some consistency in that vision: |
|
|
|
|
|
|
|
|
typedef char String20[21];
.
.
.
String20 firstName;
String20 lastName;
cin >> firstName >> lastName;
if (strcmp(lastName, "Jones") > 0)
.
.
. |
|
|
|
|
|
|
|
|
Notice how the identifier String20 suggests a maximum of 20 significant characters, and the physical size we use is 21 to allow room for the terminating null character. |
|
|
|
|
|
|
|
|
The two Problem-Solving Case Studies that follow use many of the concepts we have described in this section: string initialization, string I/O, string-handling library functions, and Typedef to define string types. |
|
|
|
|
|
|
|
|
Problem-Solving Case Study Birthday Reminder Revisited |
|
|
|
|
|
|
|
|
Problem: Rewrite the GetMonth function, from the Birthday Reminder case study in Chapter 10, so that it uses strings to convert the input month to a value of enumeration type Months. Rerun the program without making any other changes. |
|
|
|
|
|
|
|
|
Discussion: The BirthdayReminder program inputs a month and prints the names and birthdays of friends who have birthdays that month. In the original program, the characters in the month's name are read one at a time until the program recognizes the month, and any remaining characters in the month are ignored. Now that we know how to use strings, we can read the entire name of the month into a string variable and process it as a whole word rather than decoding it character by character. |
|
|
|
|
|