For this experiment, you will write code for string variables.
Strings in C++ are represented using the
Unlike integers, real numbers, and characters, strings are not built
into C++. They're added to the language through a library. A class
defines what strings are. The name of the class is also the data
type we need for our variables.
string type is defined by a class, we must load a
library to use the
string data type. The library is
string, so we have add
#include <string>to the program with the other
#includes in the file. You should also have the
using namespace std;in your program. (The original program for this experiment did.) It's good to have this line in every program, although it's only necessary for certain libraries (like
A string literal begins and ends with double quotes, with any number
of characters between them (e.g.,
string"). Incidentally, it is not necessary to include the
string library if you're only using string literals, although it
doesn't really hurt.
Write declarations for
str2, initializing them to
"Delilah", respectively. Do not get rid of any code. Compile
and execute your code.
Question #2.5.1: What is the declaration that you added to your program?
You won't see any change in the execution since the declaration and
initialization is purely internal. Once you can compile and execute
the code without problems, add a statement to print out
str2. The original program had this statement to print
cout << "i is " << i << "\n" << "j is " << j << endl;Your new statement for
str2should look quite similar, replacing the integer variables with the string variables and changing the labels appropriately.
Now compile and execute the program. Make sure it prints the proper labels and values for the variables.
Question #2.5.2: What is the ouput statement you just added to your program?
Let's play around with these declarations.
In the integer experiment, we tried initializing an integer variable
with various different types. Let's try this again for the
string variables you just added to your program.
str1 to an integer. Compile your program.
Question #2.5.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 print for the new initialization?
str1 to a double. Compile your program.
Question #2.5.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 print for the new initialization?
str1 to a character. Compile your program.
Question #2.5.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 print for the new initialization?
str1 to be equal to
cin. Compile your
Question #2.5.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 print for the new initialization?
It appears that
string doesn't like any of these other types.
You may find that your compiler gives very convoluted error messages
string is a class. Studying these messages now will
make interpreting compiler messages easier when you're not expecting
Change your program back so that
str1 is initialized to a
In the previous experiment, we took a look at escape characters.
These characters can also be used in strings:
"One line of text.\n", etc. Everything you learned
about escape characters as
chars also applies to them inside
That was easy.
As mentioned above, a string literal consists of two double quotes with any number of characters between them.
What exactly is meant by "any number of characters"? A negative number of characters seems silly. It is silly: we can't type a negative number of characters. It's more precise to say "any non-negative number of characters".
str2 with strings that had
several characters in them. The output statements in your program
have even longer string literals.
But how small can we make the strings? One character? Let's
try it. Initialize
"a", and add this output
statement to the program:
cout << "str1 has size " << str1.size() << endl;
Question #2.5.7: Make a prediction: will this compile? If so, what will it display? If not, what error message will you get?
Compile and execute the program (if you can).
Question #2.5.8: If it compiled, what did it display? If not, what error message did you get? Compare the result with your prediction.
Keep in mind that the
char type stores exactly one character,
but that's not the only way to store a character---a
string can have just one character in it. The output statements in
the program use the string literal
"\n"; as seen in the
previous experiment, this is an escape character which really counts
as one character. We could replace the double quotes
with single quotes, but we'd have to change them back if we wanted to
add more characters to those literals. Instead, let the string
literal have just one character so that we can easily add more
characters to the literal later on.
What about no characters at all? Initialize
Question #2.5.9: Make a prediction: will this compile? If so, what will it display? If not, what error message will you get?
Compile and execute the program (if you can).
Question #2.5.10: If it compiled, what did it display? If not, what error message did you get? Compare the result with your prediction.
Surprised? The literal
"" is known as the empty string. And while it
seems pointless in this program, it can be very useful when you build
up a string from smaller strings, but that's a lesson for a different