27. Program that prints the integer equivalent of a character typed at the keyboard.

C++ How to Program (8th Edition)

Chapter 2

Introduction to C++ Programming

Exercise

27. Here is a peek ahead. In this chapter you learned about integers and the type int. C++ can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. C++ uses small integers internally to represent each different character. The set of characters a computer uses and the corresponding integer representations for those characters are called that computer’s character set.You can print a character by enclosing that character in single quotes, as with
cout << 'A'; // print an uppercase A
You can print the integer equivalent of a character using static_cast as follows:
cout << static_cast< int >( 'A' ); // print 'A' as an integer
This is called a cast operation (we formally introduce casts in Chapter 4). When the preceding statement executes, it prints the value 65 (on systems that use the ASCII character set). Write a program that prints the integer equivalent of a character typed at the keyboard. Store the input in a variable of type char. Test your program several times using uppercase letters, lowercase letters, digits and special characters(like $).

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
 char ch;
 cout<<"Enter a character:"<<endl;
 cin>>ch;
 
 cout<<static_cast< int >(ch);
 
 return 0;
}

Let me know in the comment section if you have any question.

Similar Posts:

Chapter 2

  1. Program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum, product, difference, and quotient of the two numbers.
  2. Program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers separated by one space.
  3. Program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers are equal."
  4. Program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers.
  5. Program that reads in the radius of a circle as an integer and prints the circle’s diameter, circumference and area.Use the constant value 3.14159 for π. Do all calculations in output statements.
  6. Program that prints a box, an oval, an arrow and a diamond.
  7. Program that reads in five integers and determines and prints the largest and the smallest integers in the group.
  8. Program that reads an integer and determines and prints whether it’s odd or even.
  9. Program that reads in two integers and determines and prints if the first is a multiple of the second.
  10. Display checkerboard pattern with eight output statements, then display the same pattern using as few statements as possible.
  11. Program that inputs a five-digit integer, separates the integer into its digits and prints them separated by three spaces each.
  12. Program that calculates the squares and cubes of the integers from 0 to 10.

 Chapter 3: 

Comments