Console Input of Character Data in a C++ Program


Before reading this page, you should read the web page entitled C++ Console Input.

All keyboard input originates as characters, regardless of what data type is ultimately stored. For example, an integer (whole number) is entered as numerals (one type of character) and converted into a binary data format (language) used in the computer to represent integer numbers. Stream objects such as cin handle the translation required to convert input from character language into the data language of the receiving variable. For more infomation about how cin reads data from the keyboard input buffer, read the read the web page entitled C++ Console Input. The content of this page is limited exclusively to an examination of character data, both as input keystrokes and as the data type of the receiving variable, in which case no translation is necessary.

Character data is categorized in a few limited sets: letters, numerals, punctuation, and control codes. For more information about the character data type, review the web page about Data Types and Languages. The cin object can be used to read most character data without any difficulty. For example, if we wanted to store three initials (letters) into three variables defined as char variables (named: C1, C2, and C3), we could write the following C++ statements:

    cout << "Enter your first, middle and last initials (separated by blank spaces): ";
    cin >> C1 >> C2 >> C3;

A response of A[Space]B[Space]C[Enter] would store the letter 'A' in variable C1, the letter 'B' in C2, and the letter 'C' in C3 because the whitespace characters (spaces and the \n) are not stored by the cin object.

Using the cin.get() method:

When used with cin, the >> stream extraction operator uses whitespace characters (such as blanks spaces, tabs, and the \n new line character) as delimiters (separators of individual items of data). So those characters present a problem if we want to actually store them in char variables. The >> stream extraction operator does not store whitespace characters in variables; it skips over them. So if we want to store a whitespace character, we need to use a special method of the cin object named cin.get(). The purpose of this method is to read and store any single character. Consider if we wanted to store only a first and last initial and the blank space separating them into the three char variables named C1, C2, and C3, and we tried the following C++ statements:

    cout << "Enter your first and last initials (separated by one blank space): ";
    cin >> C1 >> C2 >> C3;

A response of A[Space]B[Enter] would store the letter 'A' in variable C1, the letter 'B' in C2, and nothing would be stored in C3 because the whitespace characters (spaces and the \n) are not stored by the stream extraction operator. The solution to storing the whitespace character [Space] in C2 would be to use the cin.get() method as in:

    cout << "Enter your first and last initials (separated by one blank space): ";
    cin.get(C1);
    cin.get(C2);
    cin.get(C3);

In this case, a response of A[Space]B[Enter] would store: the letter 'A' in variable C1, the blank space in C2, and the letter 'B' in C3. The whitespace characters [Space] is treated just like any other character by the cin.get() method.

Using the cin.ignore() method:

If we wanted to read from the keyboard buffer a first, middle, and last initial, but store only the first and last initials into char variables C1 and C2, skipping middle one, we could do so with the cin.ignore() method, as in the code below:

    cout << "Enter all three initials (without blank spaces): ";
    cin.get(C1);
    cin.ignore();
    cin.get(C2);

In this case, a response of ABC[Enter] would:

  1. store the letter 'A' in variable C1
  2. skip over the letter 'B'
  3. store the letter 'C' in variable C2

For more information about the methods of the cin object, see Chapter 3 of your textbook.

PATH: Instructional Server> COP 2000> Examples>