Wyo C++ - Ch. 6 Notes
Objective #1: Understand C++ strings.
- A string is a sequence of characters that may or may not spell a
word.
- A string literal is a sequence of characters that is used in a C++
program between double quotes such as in the statement
cout << "Hello world!";
where "Hello world!" is the string literal. Note that the string
literal, in this case, includes a space and an exclamation point.
- A character literal is a single character that is used in a C++ program
between single quotes. In the statement,
initial
= 'T';
the character literal is 'T'.
Objective #2: Use string objects.
- When one needs to refer to a variable that contains multiple characters,
he or she needs to use a string object. Technically, string objects
should not be called string variables. However, you may do so informally.
- You must instantiate (or declare) a string object in your program
before you wish to use it.
- Examples:
apstring studentName;
apstring
studentName = "John Doe";
- You can use the assignment operator to assign the value of one string to
another string as in:
studentName = personName;
- In order to use string objects within your program, you must use the compiler
directive,
#include "apstring.h"
at the top of your program and make sure that the apstring.h header file
is located in the same folder as your source file. At home, you can download
the apstring.h file as well as other AP class files from the AP Computer Science
Web site. You should use the compiler directive,
to include the apstring.h file when working in the Wyo computer lab.
- One very useful method that can be used with string objects is the length
method. You can determine the number of characters (letters, spaces, etc.)
in a string object with the use of a its length method. For example,
This syntax is similar to Visual Basic syntax such as:
where txtUserInput is a text box object and SetFocus is a method. However,
in C++, you must type the empty parentheses.
Objective #3: Use the output operator ( << ) for console output.
- The operator << is known as the output operator or the
insertion operator (the Knowlton textbook is incorrect on p. 98 when it
refers to << as the extraction operator.)
- You use the output operator in statements like,
cout << "Hello
world";
which would cause the string literal "Hello world" to appear on
the screen.
- cout is a stream which represents data flowing from the program
to the standard output device (typically the computer monitor/screen).
The c in cout stands for console and the letters o-u-t stand for output.
- Since the statement, cout
<< "Hello world";
, causes data to flow from your program out to the screen, the << is
called the output operator. Another way to look at it however is that
the statement above inserts information ("Hello world") into the
stream which is directed to the console out device (i.e. the monitor's screen.)
Therefore the << is also called the insertion operator.
Objective #4: Use the input operator ( >>) for console input.
- The operator >> is known as the input operator. It is also
known as the extraction operator (the Knowlton textbook is incorrect
on p. 98 when it refers to >> as the insertion operator.)
- You use the input operator in statements like,
cin >> numItems;
which would allow the user to input a value to be stored in the variable numItems.
- cin is a stream of data flowing from the keyboard into your
program. The keyboard is considered to be the standard input device. Console
I/O refers to using the keyboard and screen as the standard input and
output devices, respectively.
- Since the statement, cin
>> numItems;
, causes data to flow from the user's keyboard into the variable numItems,
the >> is called the input operator. Another way to look at it
however is that the statement above extracts information (a value inputted
by the user) from the stream which is received from the console in device
(i.e. the keyboard.) Therefore the >> is also called the extraction
operator.
- In order to end a line in an output statement you may use the new line character
( \n ) or the endl manipulator.
- the new line character, \n
cout << "Hello world" << '\n';
cout << "Hello world" << "\n";
cout << "Hello world\n";
(which are all equivalent)
- endl
cout << "Hello world" << endl;
- Other useful "escape sequences" (since the \ is the escape
operator) are:
\t to generate a tab
\\ to print a backslash
\' to print a single quote
\" to print a double quote
- The setprecision manipulator allows you to limit the number of digits
that are displayed when a numeric data type is displayed. For example,
cout << setprecision(2) << price
<< '\n';
only allows the leading two digits of the value stored in the variable,
price, to be displayed. However, if you first used the statement,
cout.setf(ios::fixed);
in your program, setprecision(2) would round values to the second decimal
place.
- The setw manipulator controls the width of the field
when displaying a value. The statement
cout << setw(10) <<
billGatesWealth << endl;
sets the width of the field allocated for the variable, billGatesWealth,
to a 10 characters.
- You may use cin to allow the user to input several items with one C++ statement.
The user however must type one or more spaces or carriage returns between
each separate inputted value. Example:
cin >> game1 >> game 2 >>
game3;
However, this is discouraged since prompts cannot be placed with each value
to be inputted. I prefer the input statements to have accompanying prompts
as in:
Objective #5: Be familiar with built-in C++ character arrays but appreciate
that it is wiser and more efficient to use a string class (such as apstring).
- There is no reserved C++ data type that is used to store sequences of characters.Without
using a string class, one needs to use an array of characters (or simply
called a character array) to store multiple characters in one variable.
- A null terminator ('\0') is stored in the last position of a character
array so that C++ knows where the array actually ends.
- You must declare a character array in your program before you wish to use
the variable.
- Example: char studentName[21];
allocates 20 positions for characters to be stored, reserving one position
for the required null terminator.
- Example: char
studentName[21] = "John Doe";
declares AND initializes the variable, studentName, in the same statement.
- Example: char
studentName[] = "John Doe";
declares and initializes the variable, studentName, using only as much
space as necessary to store the characters and the null terminator.
- Do not allocate many more positions to a character array when declaring
unless you believe that, at some point in your program, you are likely to
use most of the space. At the same time, be sure to allocate enough
space or you may overwrite other variables during program execution. This
is due to the way memory is allocated and used by C++. The errors that occur
if you are not careful are hard to detect since they may not crash your program.
- The assignment operator ( = ) will not work with C++ character arrays as
it does with apstring objects. So you must use the C++ strcpy function
in order to assign a string literal to a character array, after the character
array has already been declared. Since the statement
studentName = "John Doe";
would cause an error, you would
use the statement
strcpy(studentName, "John Doe");
In order to use the strcpy function within your program, you must use the
following compiler directive at the top of your program.
#include <string>
- You can use the output operator << to output a character array just
as you can with apstrings.
Example: cout << studentName;
- You can use the input operator >> to obtain input and store it in
a character array.
Example: cin >> studentName;
but anything typed after a space in a user's input will not be stored in the
variable studentName. Actually, any non-whitespace characters entered after
the space will still be in the C++ input stream and could corrupt logic elsewhere
in your program. So be careful if you insist upon using built-in C++ character
arrays instead of a string class for your string variables.
- If you need to have the user input a set of characters that may include
a space (such as their full name), you must use the get function as
in:
cin.get(studentName, 20);
which specifies that the first 19 characters entered by the user (remember
to allow for the null terminator) are stored in the character array studentName.
- However, if the user (unknowingly or purposefully) enters more than 19 characters
as in the example above, you must "flush the input stream". To do
this you must follow the get statement with a statement that uses the ignore
function as in
cin.ignore(80, '\n');
This will "flush the buffer" (that is, input stream). It causes
the next 80 characters in the stream to be ignored or every character up
to the next new line character ( \n ) which is created when the user presses
the Enter key.