Characters are stored as their ASCII (American Standard Code for
Information Interchange) values, i.e., integers in the range 0 through
127. Thus, each character takes up 8 bits, or one byte. C++ allows the
programmer to work with characters as characters, e.g., 'a'
or as integers. Note that the ASCII value of the character
'0'
is not the value 0. In the ASCII code, the lower case
characters are all
contiguous, the upper case characters are all contiguous, and the
digits (as characters) are all contiguous, although these three sets
of characters are not next to each other.
Usually, a string in C++ is represented as an array of
characters. If the string has n
characters, the array
that is used to store the string must have space for at
least n+1
characters because C++ uses an end of string
character at the ends of strings. The end of string character is the
character with ASCII value 0, also represented in programs
as '\0'
.
Two member functions of the cin
object of C++ are
typically used to read character input. The functions
are get
and getline
. Typically, get
is used to
read the input one character at a time, and getline
is
used to read one line of input (terminated by the new line
character).
get
takes a variable of
type char
as a
reference parameter and returns the next character from the input
stream in this variable. Thus, for example,
char nextCh; cin.get(nextCh);will return the next character from the input stream in
cout
object of C++. For
example, the first three lines of the following code will print the
character, the decimal representation
integer value (ASCII value) of the character and the hexadecimal
representation of the integer value of the
character. hex
is a C++ object in the std
namespace the directs the cout
object to output the
next integer in hexadecimal instead of decimal.
cout << nextCh << endl; cout << (int) nextCh << endl; cout << "0x" << std::hex << (int) nextCh << endl; cout << std::hex << nextCh << endl;What do you think the last line will print?
'a'
, 'A'
,
and '0'
. Verify that the decimal values are 97, 65 and
48, respectively.
nextCh
. Consider the following:
char newChar; newChar = (int) nextCh + 1; cout << newChar << endl;What do you think the above program fragment, if added to your program will do? Verify your answer by including this fragment in your code and running it.
'a'
(i.e., ASCII value 97 in decimal), then
assigning the integer value 65 (i.e., ASCII value
of 'A'
) to the variable and then printing it should
print the character 'A'
. Is that the case? Write a
program that declares a char
variable, assigns the
value 65 to it and prints it to verify.
'\0'
).
getline
takes two parameters: an array of
characters, say Line
and the size of the array,
say MAXCHAR
. It returns the characters of the next line
of standard input (delimited by the newline character) in the
array Line
. If the number of characters on teh line
exceeds MAXCHAR
, the first MAXCHAR
characters of the line are returned in Line
. The
array Line
has an end of string character as the
character after all the characters read from standard input. Write a
complete C++ program to verify the behaviour
of getline
. The member function gcount
(without any parameters) of
the cin
object
returns the number of characters that were
read by getline
. For example,
#define MAXCHAR 5 char Line[5]; cin.getline(Line, MAXCHAR); cout << "*" << Line << "*" <<" read " << cin.gcount() << " characters." << endl;will print out the line that was read and the number of characters read. (The output line prints out stars around the string just so we can see exactly the string that is being output. This becomes important while debugging programs where the strings may or may not contain the blank character or unprintable characters like control characters.) Test it by giving as input a line with fewer than 5 characters, 5 characters and more than 5 characters. What do you notice about the number of characters read? What is the explanation?
control-d
. After an attempt has been made to
read input (using cin
by itself or with a member
function as above), if the character read was the end of file
character, then the member function eof
(no
parameters) of the cin
object returns the
value true
. Thus, when reading
input until the end of file, you read the next input, check
if eof
returns true
, and if not, then
process the input.
Write a complete C++ program that prints out each line that the
user enters, and finally the number of lines
that the user enters. (The user indicates end of file by
entering control-d
. In your program you do not check
for the control-d
character, but call the member
function eof
.)
'-'
character).