Programming and Graphics

(Kiana) #1

3.4 Keyboard and monitor 63


cout << endl;
return 0;
}

The output of the code is:


<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_‘abcde

Note that, although the character variableais evaluated as an integer, it is
printed as a character through the output.


Exactly the same output would have been obtained if theforloop were
replaced either by:


for (i=60; i<=101; i++)
{
cout << (char) i;
}

or by:


for (i=60; i<=101; i++)
{
cout << char(i);
}

The statements(char) iandchar(i)invoke integer-to-character conversion
functions that perform an operation known astypecasting.


To further illustrate that characters are stored as ASCII encoded integers,
we consider the instructions:


char d = 66;
char e = ’B’;
cout << d << e << endl;

The screen display is:


BB

Peculiarities of the input buffer


The following code asks for the user’s name and age, and then prints the
information on the screen:

Free download pdf