The word character has many meanings. One complimentary definition is used to denote a person with good character. A less complimentary meaning, which I heard more than once from my parents and teachers , was that I was a character.
The word character has a third meaning in programming, though ”as a data type. As you learned in Chapter 2, each printable (letter, digit, punctuation) or whitespace ( ENTER , TAB , SPACEBAR ) key on the keyboard has a corresponding ANSI, ASCII, or Unicode value. Thus, you can assign any single user input to a character variable without fear of a data type mismatch.
Of course, often a user s input may consist of more than one character. As you learned in Chapter 10, individual characters may be organized together as a character array. Usually, a character array is ended by a null character, so its value can be outputted by the cout object and the stream insertion operator (<<). Such a null- terminated character array often is referred to as a C-string. The C in C-string refers to this character array being used in the C programming language, which was the predecessor to the C++ programming language.
C-strings often are used for data entry. For example, if the user is supposed to enter a four-digit number, but instead enters Jeff, an attempt to assign that input directly to an integer variable will result in either a run-time error or the integer variable having a so-called garbage value such as “858993460. However, if the user input is first assigned to a five-element character array (the fifth element for the null character), no run-time error would occur since any input can be represented as a character, and each character in the character array can be verified as a digit. If proper input is verified , then you can use standard library functions, as I will show you in this chapter, to convert the character array representation of an integer, long, or float value into an actual integer, long, or float value.
The C++ programming language introduced the string data type, also referred to as the C++ string class. The C++ string class often is used instead of a C-string. The functions used by C-strings and the C++ string class, respectively, will be compared and contrasted in this chapter.