Lab 2: Experiment 2


The Program

For this experiment, we will play around with the integer declarations in the original program.

The integer data type in C++ is denoted with the int keyword. This is the type used in the declaration of integer variables.

Integer Initialization

From the first experiment, you know that you can change the initial values of i and j by changing the initialization expression. What happens though, if the initialization expression is not an integer?

Change the initialization expression of i to be a literal string instead of an integer. A string begins and ends with double quotes:

"this is a string"

Consider: if someone told you they were going to give you a hug, but then kicked you, you'd complain, right? Well, the compiler probably doesn't have the same emotional connection to an int that you might have to a hug, but it's still going to complain.

Compile the program.

Question #2.2.1: What is the first error message that the compiler gives you?

Seems like the compiler will save you from making an initialization mistake. The problem here is that a string of characters cannot be turned into an integer very easily. Ask yourself: what is the best integer to represent "this is a string"?

But what if the data types were very similar and could be converted easily?

Change the initialization expression of i to be a literal character. A character is a single character surrounded by single quotes: 'f'.

Compile the program.

Question #2.2.2: What is the first error message that the compiler gives you?

You might be very confused at this point because your compiler didn't give you an error message. Some don't. (Answer the previous question appropriately: "I didn't get an error message.") The int and char data types in C++ are very closely related, and you probably won't get an error for initializing an int with a char. This is because a single char is represented in the computer's memory as an integer. So it's very easy for the compiler to switch between the two.

Some compilers will issue a warning for this initialization since often this is a programming mistake; either i should be declared as a char, or the initialization is wrong. Either way, it's a warning you should watch out for, just like any compiler error.

What about a real number, something with a decimal point in it? Change the initialization of i to be 3.14159 (the most overused real-number literal in programming examples), and compile the program.

Question #2.2.3: What is the first error message or warning that the compiler gives you?

Again, you probably don't get a error message, but you might have gotten a warning.

Let's try something really daring: cin. Initialize i to be equal to cin. It's a completely ridiculous thing to do, but let's see what the compiler does. Compile the program.

Question #2.2.4: What is the first error message or warning that the compiler gives you?

This time you should get an actual error message.

Finish

Restore your program so that i is initialized to a proper integer.


Back to the Lab Exercise  |  Forward to the Next Experiment
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.