For this experiment, you will write code dealing with character variables.
The keyword for the character data type in C++ is char
. It
represents exactly one character.
A character literal begins and ends with single quotes, with the
character between them (e.g., 'a'
and 'A'
).
Write declarations for char
variables ch1
and ch2
, initializing them to 'a'
and 'Z'
, respectively.
Do not get rid of any code. Compile and execute your program.
Question #2.4.1: What is the declaration that you added to your program?
Compile your modified program. Once it compiles without errors,
add a statement to display ch1
and ch2
. It should
be similar to the statement in the original program to display
count
and number
:
cout << "count is " << count << "\n" << "number is " << number << endl;
Now compile and execute the program. Make sure it outputs the proper labels and values for the variables.
Question #2.4.2: What is the ouput statement you just added to your program?
In the integer experiment, we tried initializing an integer variable
with various different types. Let's try this again for the char
variables you just added to your program. Again, "I didn't get
any compiler errors or warnings" is an acceptible answer for these
questions.
Initialize ch1
to an integer (try something between 64 and
100). Compile your program.
Question #2.4.3: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization when you execute it?
Initialize ch1
to a double. Compile your program.
Question #2.4.4: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization when you execute it?
Initialize ch1
to a string. Compile your program.
Question #2.4.5: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization when you execute it?
Initialize ch1
to be equal to cin
. Compile your
program.
Question #2.4.6: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization when you execute it?
Change your program back so that ch1
is initialized to a
character.
As mentioned above, a character literal consists of two single quotes with a character between them.
What happens if there's more than one character between them?
Change the character literal for ch1
to be 'abc'
.
Compile your program.
Question #2.4.7: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization?
There are some characters that perform an action. For example, if you want output to start on a new line, you must use the newline character. However, we're using the newline character (when we press the Return key) in our programs to begin a new line of code in the program. So how do we produce a new line in a program's output?
C++ accomplishes this by providing
escape characters to produce special
effects in a program's output. An escape character
begins with the backslash character, \
, followed by another
character that represents the special effect in the output.
For example, the newline character literal is '\n'
.
To specify it in a program, we use two separate characters, but the C++
compiler interprets them as a single character, the newline character.
Some other escape characters include '\t'
for the tab
character, '\''
for a single quote, '\"'
for a
double quote (more useful in strings), and '\\'
for a
backslash character.
In your program, initialize ch1
to be the newline
character and ch2
to be a single quote.
Question #2.4.8: Predict: what will your program display when you execute it?
Compile and execute your program to test your prediction.
Question #2.4.9: What did your program actually display? Does this agree with your prediction?
As you will see in the next experiment, these escape characters can also be used in strings.