&&, ||, and
!
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.
==, <, >, 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;
}
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.
(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";
}
|
cin and
cout.
x is not a good name, but InputLetter is.
// Course: CSC 306 Introduction to Programming with C++
// Name: Your Name
// Assignment #5: tOGGLE
/*
Purpose: <Put a more in-depth description of the program here.>
*/
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.