The three subjects of this chapter were the character data type, a C-String (a null- terminated character array), and the C++ string class.
We first discussed the issues involved in reading a character when a newline character may be left in the input buffer, and derived the following three rules.
The first rule is to always follow cin and the stream extraction operator (>>) with the no-argument ignore member function. The reason is that the cin object with the stream extraction operator >> always leaves a newline character in the input buffer, so calling the no-argument ignore member function will dispose of that leftover newline character.
The second rule is not to use the ignore member function after using the getline member function. The reason is that the getline member function removes the newline character that terminated input from the input buffer.
The third rule is that after using the get member function with one character argument, such as cin.get(ch), check to see if you have a newline character in the input buffer, and if you do, clear it with the ignore member function. If you don t, don t. The reason is that the get member function with one argument being a single character will leave a newline character in the input buffer if you type a character and then press ENTER , but it won t if you just press ENTER .
The C++ standard library cctype contains a number of functions that are useful with characters . The toupper and tolower functions each have one argument, a character. If the character represents a letter, A through Z or a through z, the toupper function returns the uppercase of that letter. Conversely, the tolower function returns the lowercase of that letter. If the argument is not a letter, both functions simply return the same character that is the argument. A number of other functions in the cctype standard library, including but not limited to isalpha and isdigit , permit you to determine the value of a character, such as whether it is a letter, digit, or whitespace.
The cstring standard library provides a number of functions that are useful when working with C-strings. Similarly, the string standard library provides a number of functions that are useful when working with the C++ string class.
You use the strlen function to determine the length of a C-string, the length or size member functions to return the length of a C++ string class variable. You use the strcpy function to assign a value to a C-string, and the assignment operator = to assign a value to a C++ string class variable. You use the strcat function to append a value to a C-string, and the combined addition and assignment operator += to append a value to a C++ string class variable. You use the strcmp function to compare two C-strings, and the relational operators to compare two C++ string class variables .
The cstdlib standard library provides several functions for converting the C-string representation of a number to a numeric data type, as well as the reverse. These are atoi, or ASCII to integer, atol, or ASCII to Long, atof, or ASCII to float, and itoa, or Integer to ASCII.