CSC 306 Introduction to Programming with C++

More About C++ Data Types

Chapter 2.4 (pages 68-72), 3.2 (pages 104-105)


Objectives

Important Definitions


ASCII and the char Data Type in C++

Variables of type char are used to store one single character of data, such as a letter, or a digit, or a blank space, etc. These characters come from an available character set which can differ from computer to computer in different countries. However, it always includes upper and lower case letters of the alphabet, the digits 0, ... , 9, and some special symbols such as #, £, !, +, -. The American Standard Code for Information Interchange (ASCII) character set is a standard seven-bit binary code that was proposed by ANSI in 1963 and finalized in 1968. ASCII, pronounced "ask-key", was established to achieve compatibility between various types of data processing equipment, and thus is the most common character code for computer equipment. The standard ASCII character set consists of 128 decimal numbers ranging from zero through 127, and each are assigned to letters, numbers, punctuation marks, and the most common special characters. The extended ASCII character has an additional 128 decimal numbers that range from 128 through 255 representing additional special, mathematical, graphic, and foreign characters.

ASCII characters can either be displayed in decimal (base 10) or in hexadecimal (base 16). See the tables at ASCII.

Character constants of type char must be enclosed in single quotation marks when used in a program, otherwise the compiler will likely misinterpret them and result in a compilation error or unexpected program behaviour. For example, 'A' is a character constant, but A is a program variable, and similarly '9' is a character, but 9 is an integer. A string, such as "Please enter a number: ", a represented as a sequence of zero or more characters and must be enclosed in double quotation marks. Strings, unlike int or char, are not a basic C++ data type, and will be covered in more detail later.

Operations on chars in C++

The same mathematical operations that work on integers also work on characters. The operations of + and - , as well as the conditional operators such as ==, <, >, are very useful and serve an intuitive purpose. Consider the following example in which 1 is added the the character 'A':
    char letter;
    letter = 'A' + 1;
    cout << letter << endl; 
The program will output the letter B because it is 1 letter after the letter A in ASCII. The following code fragment compares the letter in variable t1 and outputs "less than" if that letter is lower in the alphabet than the character 'a':
    char letter;
    cin >> letter;
    if( letter < 'a' ) {
        cout << "less than" << endl;
    }

Type Casting in C++

Type Casting is used to convert the type of an object, expression, function argument, or return value to that of another type. Some standard C++ and user-defined casting conversions are performed automatically by the compiler without intervention by the programmer. These standard C++ and user-defined conversions are called implicit casting. Other conversions must be explicitly specified by the programmer and are appropriately called explicit casting. For example, the following is legal.
    int number;
    number = 'a'; // This is implicit casting of a character into an integer
    cout << number << endl; 
The result will be 97, which is the number that is used internally by C++ to represent the letter 'a' in ASCII. Implicit casting has its drawbacks, however, because it is not 100% clear what the programmer intended to happen. The following has better style, because the casting is explicit:
    int number; 
    number = static_cast<int>('a'); // Using explicit static casting
    cout << number << endl;
Again, the result will be 97, but it is much easier to see what is happening through the cast conversion.

It is generally a good idea to treat one data type consistently in a program and only convert from one to the other if there is a good reason to do so. The following program, conversion.cpp, is designed to do conversions:

    #include <iostream>
    using namespace std;

    // The program takes as input a character and displays what it is cast into 
    // an integer data type.

    int main() {
	int number;
	char character;

	cout << "Type in a character:\n";
	cin >> character;

	// explicitly cast the character into an integer to output decimal
	number = static_cast<int>(character);

	cout << "The character '" << character;
	cout << "' is represented as the ASCII decimal number ";
	cout << number << " in the computer.\n";

	int temp;
	cin >> temp;

	return 0;
    }
Given the character '9' as input, the program outputs:
The character '9' is represented as the ASCII decimal number 57 in the computer.

The program below, hex.cpp, is a modification of the above program so that it prints out the whole ASCII table of characters (note the use of a "while loop"). Here, we force C++ to manipulate the output to produce the ASCII code in hexadecimal instead of decimal.

    #include <iostream>
    using namespace std;
    
    int main() {

	int number =32;  // The ASCII printable characters begin at 32.
	char character;

	// The printable characters end at 126
	while (number <= 126) {

	    character = static_cast <char>(number); //Here we cast number to a char.
	    cout << "The character '" << character;
	    cout << dec << number << " in decimal or "; //cout in decimal
	    cout << dec << number << " decimal or ";
	    cout << hex << number<< " in hexadecimal.\n"; //cout in hex
	    number++;  //This increments number by 1 each iteration.
	}

	return 0;
    }
This program produces the output:
    The character ' ' is represented as 32 in decimal or 20 in hexadecimal.
    The character '!' is represented as 33 in decimal or 21 in hexadecimal.
    ...
    ...
    The character '}' is represented as 125 in decimal or 7D in hexadecimal.
    The character '~' is represented as 126 in decimal or 7E in hexadecimal.

Logical Operators

There are three logical operators in C++: AND, OR and NOT, which are denoted by the symbols &&, || and !. The semantics (meaning) of these operators is similar to their meaning in English. For example, (num > 0 && num < 10) is true only if num is greater than zero AND less than 10.

Logical operators often provide a way to simplify nested conditional statements. For example, consider the following nested conditional transformation:

Nested Conditional Using Logical Operators
  if( num > 0) { 
    if( num < 10 ) { 
	cout << "positive single digit.\n"; 
    }
  }
  if ((num > 0) && (num < 10)) { 
    cout << "num is a positive single digit.\n"; 
  }
In addition to their use in the conditions of conditional statements, logical operators can also be used in the conditions of loop tests. We will learn further uses of logical operators, including the use of "!" later in the course.

User Defined Data Types

Later in the course we will also study the topic of data types in much more detail, and we will see how the programmer may define his or her own data types. This facility provides a powerful programming tool when complex structures of data need to be represented and manipulated by a C++ program.


Assignment Specifics

In Microsoft Word, you may have noticed the "Change Case" under the Format menu. One of the options in this is to tOGGLE the case of a letter or a set of letters. Your assignment today is to write a program that toggle the "case" of a letter as follows. Note: This assignment is to be completed individually.

  1. Your program should welcome the user to your tOGGLE cASE program, but this welcome should appear only once regardless of how many letters the user inputs to toggle its case.

  2. The program should then ask the user to enter a single letter.

  3. If the user types in a letter that is upper case, the program should output it in lower case in appropriately formatted informative output.

  4. If the user types in a letter that is in lower case, the program should output it in upper case in appropriately formatted informative output.

  5. If the user enters a character that is not a letter, then the program should echo the character entered and then inform the user that it is not a letter.

  6. The program should ask the user if he or she has another letter to convert. An answer of Y or y should cause the program to loop back to step 3.
    Any other answer should cause your program to thank the user for tOGGLing and stop.

Notes

When you are finished writing and testing your assignment, drop your source code, YourLastName_306A5.cpp, into the CSC306_A05 dropbox on the Academic server.


Back to Introduction to Computer Programming with C++ Homepage