2

C++ Syntax and Semantics, and the Program Development Process

KNOWLEDGE GOALS

image  To understand how a C++ program is composed of one or more subprograms (functions).

image  To know what a metalanguage is, and how it is used.

image  To understand the concept of a data type.

image  To learn the steps involved in entering and running a program.

SKILL GOALS

To be able to:

image  Read syntax templates so as to understand the formal rules governing C++ programs.

image  Create and recognize legal C++ identifiers.

image  Declare named constants and variables of type char and string.

image  Distinguish reserved words in C++ from user-defined identifiers.

image  Assign values to variables.

image  Construct simple string expressions made up of constants, variables, and the concatenation operator.

image  Construct a statement that writes to an output stream.

image  Determine what is printed by a given output statement.

image  Use comments to clarify your programs.

image  Construct simple C++ programs.

2.1 The Elements of C++ Programs

Programmers develop solutions to problems using a programming language. In this chapter, we start looking at the rules and symbols that make up the C++ programming language. We also review the steps required to create a program and make it work on a computer.

C++ Program Structure

In Chapter 1, we talked about the four basic structures for expressing actions in a programming language: sequence, selection, loop, and subprogram. We said that subprograms allow us to write parts of our program separately and then assemble them into final form. In C++, all subprograms are referred to as functions, and a C++ program is a collection of one or more functions.

Each function performs some particular task, and collectively they all cooperate to solve the entire problem.

Function A subprogram in C++.

image

Every C++ program must have a function named main. Execution of the program always begins with the main function. You can think of main as the master and the other functions as the servants. When main wants the function Square to perform a task, main calls (or invokes) Square. When the Square function completes execution of its statements, it obediently returns control to the master, main, so the master can continue executing.

Let’s look at an example of a C++ program with three functions: main, Square, and Cube. Don’t be too concerned with the details in the program—just observe its overall look and structure.

image

image

In each of the three functions, the left brace ({) and the right brace (}) mark the beginning and the end of the statements to be executed. Statements appearing between the braces are known as the body of the function.

Execution of a program always begins with the first statement of the main function. In our program, the first statement is

cout << "The square of 27 is " << Square(27) << endl;

This output statement causes information to be printed on the computer’s display screen. You will learn how to construct output statements later in the chapter. Briefly, this statement prints two items. The first is the message

The square of 27 is

The second item to be printed is the value obtained by calling (invoking) the Square function, with the value 27 as the number to be squared. As the servant, the Square function performs its task of squaring the number and sending the computed result (729) back to its caller, the main function. Now main can continue executing by printing the value 729 and proceeding to its next statement.

In a similar fashion, the second statement in main prints the message

and the cube of 27 is

and then invokes the Cube function and prints the result, 19683. The complete output produced by executing this program is, therefore,

image

Both Square and Cube are examples of value-returning functions. A value-returning function returns a single value to its caller. The word int at the beginning of the first line of the Square function

int Square( int n )

states that the function returns an integer value.

Now look at the main function again. You’ll see that the first line of the function is

int main()

The word int indicates that main is a value-returning function that should return an integer value. And it does: After printing the square and cube of 27, main executes the statement

return 0;

to return the value 0 to its caller. But who calls the main function? The answer: the computer’s operating system.

When you work with C++ programs, the operating system is considered to be the caller of the main function. The operating system expects main to return a value when main finishes executing. By convention, a return value of 0 means everything went okay. A return value of anything else (typically 1, 2, …) means something went wrong. Later in this book we look at situations in which you might want to return a value other than 0 from main. For the time being, we always conclude the execution of main by returning the value 0.

We have looked only briefly at the overall picture of what a C++ program looks like—a collection of one or more functions, including main. We have also mentioned what is special about the main function—it is a required function, execution begins there, and it returns a value to the operating system. Now it’s time to begin looking at the details of the C++ language.

Syntax and Semantics

A programming language is a set of rules, symbols, and special words used to construct a program. In particular, there are rules for both syntax (grammar) and semantics (meaning).

Syntax The formal rules governing how valid instructions are written in a programming language.

Semantics The set of rules that determines the meaning of instructions written in a programming language.

Syntax is a formal set of rules that defines exactly which combinations of letters, numbers, and symbols can be used in a programming language. There is no room for ambiguity in the syntax of a programming language because the computer can’t think; it doesn’t “know what we mean.” To avoid ambiguity, syntax rules themselves must be written in a very simple, precise, formal language called a metalanguage.

Metalanguage A language that is used to write the syntax rules for another language.

Learning to read a metalanguage is like learning to read the notations used in the rules of a sport. Once you understand the notations, you can read the rule book. It’s true that many people learn a sport simply by watching others play, but what they learn is usually just enough to allow them to take part in casual games. You could learn C++ by following the examples in this book, but a serious programmer, like a serious athlete, must take the time to read and understand the rules.

Syntax rules are the blueprints we use to build instructions in a program. They allow us to take the elements of a programming language—the basic building blocks of the language—and assemble them into constructs, which are syntactically correct structures. If our program violates any of the rules of the language—by misspelling a crucial word or leaving out an important comma, for instance—the program is said to have syntax errors and cannot compile correctly until we fix them.

THEORETICAL FOUNDATIONS

Metalanguages

image

Metalanguage is the word language with the prefix meta, which means “beyond” or “more comprehensive.” In other words, a metalanguage is a language that goes beyond a normal language by allowing us to speak precisely about that language. It is a language for talking about languages.

One of the first computer-oriented metalanguages was the Backus–Naur Form (BNF), which is named for John Backus and Peter Naur, who developed it in 1960. BNF is an extremely simple language. When we say simple, however, we mean that the metalanguage itself has very few symbols and rules. Unfortunately, that simplicity forces each syntax definition to be written using many symbols, so that it becomes long and difficult to read. In this book, we use another metalanguage, called a syntax template. Syntax templates show at a glance the form of a C++ construct.

One final note: Metalanguages show only how to write instructions that the compiler can translate. They do not define what those instructions do (their semantics). Formal languages for defining the semantics of a programming language exist, but they are beyond the scope of this text. Throughout this book, we will describe the semantics of C++ in English.

Syntax Templates

In this book, we write the syntax rules for C++ using a metalanguage called a syntax template. A syntax template is a generic example of the C++ construct being defined. Graphic conventions show which portions are optional and which portions can be repeated. A boldface word or symbol is a literal word or symbol in the C++ language. A nonboldface word can be replaced by another template. A curly brace is used to indicate a list of items, from which one item can be chosen.

Let’s look at an example. This template defines an identifier in C++:

Identifier

image

The shading indicates a part of the definition that is optional. The three dots (…) mean that the preceding symbol or shaded block can be repeated. Thus an identifier in C++ must begin with a letter or underscore and is optionally followed by one or more letters, underscores, or digits.

Remember that a word not in boldface type can be replaced with another template. Here are the templates for Letter and Digit:

image

In these templates, the braces again indicate lists of items from which any one item can be chosen. So a letter can be any one of the uppercase or lowercase letters, and a digit can be any of the numeric characters 0 through 9.

Now let’s look at the syntax template for the C++ main function:

MainFunction

image

The main function begins with the word int, followed by the word main and then left and right parentheses. This first line of the function is the heading. After the heading, the left brace signals the start of the statements in the function (its body). The shading and the three dots indicate that the function body consists of zero or more statements. (In this diagram we have placed the three dots vertically to suggest that statements usually are arranged vertically, one above the next.) Finally, the right brace indicates the end of the function.

In principle, the syntax template allows the function body to have no statements at all. In practice, the body should include a return statement because the word int in the function heading states that main returns an integer value. Thus the shortest C++ program possible is

image

As you might guess, this program does absolutely nothing useful when executed!

As we introduce C++ language constructs throughout the book, we use syntax templates to display the proper syntax. When you finish this chapter, you should know enough about the syntax and semantics of statements in C++ to write simple programs. But before we can talk about writing statements, we must look at how names are written in C++ and at some of the elements of a program.

Naming Program Elements: Identifiers

As we noted in our discussion of metalanguages, identifiers are used in C++ to name things—things such as subprograms and places in the computer’s memory. Identifiers are made up of letters (A–Z, a–z), digits (0–9), and the underscore character (_), but must begin with a letter or underscore.

Identifier A name associated with a function or data object and used to refer to that function or data object.

Remember that an identifier must start with a letter or underscore:

Identifier

image

(Identifiers beginning with an underscore have special meanings in some C++ systems, so it is best to begin an identifier with a letter.) Here are some examples of valid identifiers:

sum_of_squares J9 box_22A GetData Bin3D4 count

And here are some examples of invalid identifiers and the reasons why they are invalid:

Invalid Identifier

Explanation

40Hours

Identifiers cannot begin with a digit.

Get Data

Blanks are not allowed in identifiers.

box-22

The hyphen (-) is a math symbol (minus) in C++.

cost_in_$

Special symbols such as $ are not allowed.

int

The word int is predefined in the C++ language.

The last identifier in the table, int, is an example of a reserved word. Reserved words are words that have specific uses in C++; you cannot use them as programmer-defined identifiers. Appendix A lists all of the reserved words in C++.

Reserved word A word that has special meaning in C++; it cannot be used as a programmer-defined identifier.

The LeapYear program in Chapter 1 uses the programmer-defined identifiers in the following list. (Most of the other identifiers in the program are C++ reserved words.) Notice that we chose the names to convey how the identifiers are used.

Identifier

How It Is Used

year

Year to be tested

IsLeapYear

Whether year is a leap year

MATTERS OF STYLE Using Meaningful, Readable Identifiers

images

The names we use to refer to things in our programs are totally meaningless to the computer. The computer behaves in the same way whether we call the value 3.14159265 pi or cake, as long as we always call it the same thing. However, it is much easier for someone to figure out how a program works if the names we choose for elements actually tell something about them. Whenever you have to make up a name for something in a program, try to pick one that is meaningful to a person reading the program.

C++ is a case-sensitive language, which means that it considers uppercase letters to be different from lowercase letters. The identifiers

PRINTTOPPORTION  printtopportion  pRiNtToPpOrTiOn  PrintTopPortion

are four distinct names and are not interchangeable in any way. As you can see, the last of these forms is the easiest to read. In this book, we use combinations of uppercase letters, lowercase letters, and underscores in identifiers. We explain our conventions for choosing between uppercase and lowercase as we proceed through this chapter.

Now that we’ve seen how to write identifiers, let’s look at some of the things that C++ allows us to name.

Data and Data Types

A computer program operates on data (stored internally in memory, stored externally on disk, or input from a device such as a keyboard, scanner, network, or electrical sensor) and produces output. In C++, each piece of data must be of a specific data type. The data type determines how the data is represented in the computer and the kinds of processing the computer can perform on it.

Data type A specific set of data values, along with a set of operations on those values.

Some types of data are used so frequently that C++ defines them for us. Examples of these standard (or built-in) types include int (for working with integer numbers), float (for working with real numbers having decimal points), and char (for working with character data).

Additionally, C++ allows programmers to define their own data types—known as programmer-defined (or user-defined) types. Beginning in Chapter 10, we show you how to define your own data types.

In this chapter, we focus on two data types—one for representing data consisting of a single character, the other for representing strings of characters. In Chapter 3, we examine the numeric types (such as int and float) in detail.

BACKGROUND INFORMATION

Data Storage

image

Where does a program get the data it needs to operate? Data is stored in the computer’s memory. Recall that memory is divided into a large number of separate locations or cells, each of which can hold a piece of data. Each memory location has a unique address to which we refer when we store or retrieve the data there. We can visualize memory as a set of post office boxes, with the box numbers as the addresses used to designate particular locations.

image

Of course, the actual address of each location in memory is a binary number in a machine language code. In C++, we use identifiers to name memory locations; the compiler then translates them into binary for us. This is one of the advantages of a high-level programming language: It frees us from having to keep track of the numeric addresses of the memory locations in which our data and instructions are stored.

The char Data Type

The built-in type char describes data consisting of one alphanumeric character—a letter, a digit, or a special symbol:

image

Each machine uses a particular character set, the set of alphanumeric characters it can represent. (See Appendix E for some sample character sets.) Notice that each character is enclosed in single quotes (apostrophes). The C++ compiler needs the quotes to differentiate, say, between the character data '8' and the integer value 8 because the two are stored differently inside the machine. Notice also that the blank, ' ', is a valid character.1

You wouldn’t want to add the character 'A' to the character 'B' or subtract the character '3' from the character '8', but you might want to compare character values. Each character set has a collating sequence, a predefined ordering of all the characters. Although this sequence varies from one character set to another, 'A' always compares less than 'B', 'B' less than 'C', and so forth. Also, '1' compares less than '2', '2' less than '3', and so on. None of the identifiers in the LeapYear program is of type char.

The string Data Type

Whereas a value of type char is limited to a single character, a string is a sequence of characters, such as a word, name, or sentence, enclosed in double quotes. For example, the following are strings in C++:

image

A string must be typed entirely on one line. For example, the string

image

is not valid because it is split across two lines. In this situation, the C++ compiler issues an error message at the first line. The message may say something like UNTERMINATED STRING, depending on the particular compiler.

The quotes are not considered to be part of the string but are simply there to distinguish the string from other parts of a C++ program. For example, "amount" (in double quotes) is the character string made up of the letters a, m, o, u, n, and t in that order. By contrast, amount (without the quotes) is an identifier, perhaps the name of a place in memory. The symbols "12345" represent a string made up of the characters 1, 2, 3, 4, and 5 in that order. If we write 12345 without the quotes, it is an integer quantity that can be used in calculations.

A string containing no characters is called the null string (or empty string). We write the null string using two double quotes with nothing (not even spaces) between them:

""

The null string is not equivalent to a string of spaces; it is a special string that contains no characters.

To work with string data, this book uses a data type named string. This data type is not part of the C++ language (that is, it is not a built-in type). Rather, string is a programmer-defined type that is supplied by the C++ standard library, a large collection of prewritten functions and data types that any C++ programmer can use. Operations on string data include comparing the values of strings, searching a string for a particular character, and joining one string to another. We look at some of these operations later in this chapter and cover additional string operations in subsequent chapters. None of the identifiers in the LeapYear program is of type string, although string values are used directly in several places in the program.

Naming Elements: Declarations

Identifiers can be used to name both constants and variables. In other words, an identifier can be the name of a memory location whose contents are not allowed to change or it can be the name of a memory location whose contents can change.

How do we tell the computer what an identifier represents? By using a declaration, a statement that associates a name (an identifier) with a description of an element in a C++ program (just as a dictionary definition associates a name with a description of the thing being named). In a declaration, we name an identifier and indicate what it represents. For example, the LeapYear program uses the declaration

Declaration A statement that associates an identifier with a data object, a function, or a data type so that the programmer can refer to that item by name.

int year;

to announce that year is the name of a variable whose contents are of type int. When we declare a variable, the compiler picks a location in memory to be associated with the identifier. We don’t have to know the actual address of the memory location because the computer automatically keeps track of it for us.

Suppose that when we mailed a letter, we only had to put a person’s name on it and the post office would look up the address. Of course, everybody in the world would need a different name; otherwise, the post office wouldn’t be able to figure out whose address was whose. The same is true in C++. Each identifier can represent just one thing (except under special circumstances, which we talk about in Chapters 7 and 8). Every identifier you use in a program must be different from all others.

Constants and variables are collectively called data objects. Both data objects and the actual instructions in a program are stored in various memory locations. You have seen that a group of instructions—a function—can be given a name. A name also can be associated with a programmer-defined data type.

In C++, you must declare every identifier before it is used. This practice allows the compiler to verify that the use of the identifier is consistent with what it was declared to be. If you declare an identifier to be a constant and later try to change its value, for example, the compiler detects this inconsistency and issues an error message.

There is a different form of declaration statement for each kind of data object, function, or data type in C++. The forms of declarations for variables and constants are introduced here; others are covered in later chapters.

Variables

A program operates on data, which is stored in memory. While a program is executing, different values may be stored in the same memory location at different times. This kind of memory location is called a variable, and its content is the variable value. The symbolic name that we associate with a memory location is the variable name or variable identifier (see FIGURE 2.1). In practice, we often refer to the variable name more briefly as the variable.

Variable A location in memory, referenced by an identifier, that contains a data value that can be changed.

Declaring a variable means specifying both the variable’s name and its data type. The declaration tells the compiler to associate a name with a memory location whose contents are of a specific type (for example, char or string). The following statement declares myChar to be a variable of type char:

char myChar;

image

FIGURE 2.1 Variable

In C++, a variable can contain a data value only of the type specified in its declaration. Because of the preceding declaration, the variable myChar can contain only a char value. If the C++ compiler comes across an instruction that tries to store a float value into myChar, it generates extra instructions to convert the float value to the proper type. In Chapter 3, we examine how such type conversions take place.

Here’s the syntax template for a variable declaration:

VariableDeclaration

image

where DataType is the name of a data type such as char or string. Notice that a variable declaration always ends with a semicolon.

From the syntax template, you can see that it is possible to declare several variables in one statement:

char letter, middleInitial, ch;

Here, all three variables are declared to be char variables. Our preference, though, is to declare each variable with a separate statement:

image

This form makes it easier, when modifying a program, to add new variables to the list or delete ones you no longer want.

Declaring each variable with a separate statement also allows you to attach comments to the right of each declaration, as shown here:

image

These declarations tell the compiler to reserve memory space for three float variables—payRate, hours, and wages—and one int variable, empNum. The comments explain to someone reading the program what each variable represents.

Now that we’ve seen how to declare variables in C++, let’s look at how to declare constants.

Constants

All single characters (enclosed in single quotes) and strings (enclosed in double quotes) are constants.

image

In C++, as in mathematics, a constant is something whose value never changes. When we use the actual value of a constant in a program, we are using a literal value (or literal).

Literal value Any constant value written in a program.

An alternative to the literal constant is the named constant (or symbolic constant), which is introduced in a declaration statement. A named constant is just another way of representing a literal value. Instead of using the literal value in an instruction, we give it a name in a declaration statement, then use that name in the instruction. For example, we can write an instruction that prints the title of this book using the literal string "Programming and Problem Solving with C++". Alternatively, we can declare a named constant called BOOK_TITLE that equals the same string and then use the constant name in the instruction. That is, we can use either

Named constant (symbolic constant) A location in memory, referenced by an identifier, that contains a data value that cannot be changed.

"Programming and Problem Solving with C++"

or

BOOK_TITLE

in the instruction.

Using a literal value may seem easier than declaring a named constant and then referring to it. But, in fact, named constants make a program easier to read because they make the meaning clearer. Named constants also make it easier to change a program at a later time.

Here is the syntax template for a constant declaration:

ConstantDeclaration

image

Notice that the reserved word const begins the declaration, and an equal sign (=) appears between the identifier and the literal value.

The following are examples of constant declarations:

image

As we have done above, many C++ programmers capitalize the entire identifier of a named constant and separate the English words with an underscore. The idea is to let the reader quickly distinguish between variable names and constant names when they appear in the middle of a program.

It’s a good idea to add comments to constant declarations as well as variable declarations. We describe in comments what each constant represents:

image

Here is a program that contains just declarations:

image

MATTERS OF STYLE

Capitalization of Identifiers

image

Programmers often use capitalization as a quick visual clue to what an identifier represents. Different programmers adopt different conventions for using uppercase letters and lowercase letters. Some people use only lowercase letters, separating the English words in an identifier with the underscore character:

image

The conventions we use in this book are as follows:

image  For identifiers representing variables, we begin with a lowercase letter and capitalize each successive English word.

image

image  Names of programmer-written functions and programmer-defined data types (which we examine later in the book) are capitalized in the same manner as variable names, except that they begin with capital letters.

image

Capitalizing the first letter allows a person reading the program to tell at a glance that an identifier represents a function name or data type rather than a variable. However, we cannot use this capitalization convention everywhere. C++ expects every program to have a function named main—all in lowercase letters—so we cannot name it Main. Nor can we use Char for the built-in data type char. C++ reserved words use all lowercase letters, as do most of the identifiers declared in the standard library (such as string).

image  For identifiers representing named constants, we capitalize every letter and use underscores to separate the English words.

image

This convention, which is widely used by C++ programmers, is an immediate signal that BOOK_TITLE is a named constant and not a variable, a function, or a data type.

These conventions are only that—conventions. In other words, C++ does not require this particular style of capitalizing identifiers. You may wish to capitalize identifiers in a different fashion. But whatever system you use, it is essential that you use a consistent style throughout your program. A person reading your program will be confused or misled if you use a random style of capitalization.

Taking Action: Executable Statements

Up to this point, we’ve looked at ways of declaring data objects in a program. Now we turn our attention to ways of acting, or performing operations, on data.

Assignment

The value of a variable can be set or changed through an assignment statement. For example,

Assignment statement A statement that stores the value of an expression into a variable.

image

assigns the string value "Lincoln" to the variable lastName; that is, it stores the sequence of characters "Lincoln" into the memory associated with the variable named lastName.

Here’s the syntax template for an assignment statement:

AssignmentStatement

image

The semantics (meaning) of the assignment operator (=) is “store”; the value of the expression is stored into the variable. Any previous value in the variable is destroyed and replaced by the value of the expression.

Expression An arrangement of identifiers, literals, and operators that can be evaluated to compute a value of a given type.

Only one variable can be on the left-hand side of an assignment statement. An assignment statement is not like a math equation (x + y = z + 4); the expression (what is on the right-hand side of the assignment operator) is evaluated, and the resulting value is stored into the single variable on the left of the assignment operator. A variable keeps its assigned value until another statement stores a new value into it.

Evaluate To compute a new value by performing a specified set of operations on given values.

Given the declarations

image

the following assignment statements are valid:

image

However, these assignments are not valid:

Invalid Assignment Statement

Reason

middleInitial = "A.";

middleInitial is of type char; "A." is of type string.

letter = firstName;

letter is of type char; firstName is of type string.

firstName = Thomas;

Thomas is an undeclared identifier.

"Edison" = lastName;

Only a variable can appear to the left of =.

lastName = ;

The expression to the right of = is missing.

String Expressions

Although we can’t perform arithmetic on strings, the string data type provides a special string operation, called concatenation, that uses the + operator. The result of concatenating (joining) two strings is a new string containing the characters from both strings. For example, given the statements

image

we could write

bookTitle = phrase1 + phrase2;

This statement retrieves the value of phrase1 from memory and concatenates the value of phrase2 to form a new, temporary string containing the characters

"Programming and Problem Solving"

This temporary string (which is of type string) is then assigned to (stored into) book-Title.

The order of the strings in the expression determines how they appear in the resulting string. If we write

bookTitle = phrase2 + phrase1;

then bookTitle contains

"Problem SolvingProgramming and"

Concatenation works with named string constants, literal strings, and char data, as well as with string variables. The only restriction is that at least one of the operands of the + operator must be a string variable or named constant (so you cannot use expressions like "Hi" + "there" or 'A' + 'B'). For example, if we have declared the following constants

image

then we could write the following assignment statement to store the title of this book into the variable bookTitle:

image

As a result, bookTitle contains the string

image

The preceding example demonstrates how we can combine identifiers, char data, and literal strings in a concatenation expression. Of course, if we simply want to assign the complete string to bookTitle, we can do so directly:

image

Occasionally we may encounter a situation in which we want to add some characters to an existing string value. Suppose that bookTitle already contains "Programming and Problem Solving" and now we wish to complete the title. We could use a statement of the form

image

Such a statement retrieves the value of bookTitle from memory, concatenates the string "with C++" to form a new string, and then stores the new string back into bookTitle. The new string replaces the old value of bookTitle (which is destroyed).

Keep in mind that concatenation works only with values of type string. Even though an arithmetic plus sign is used for the operation, we cannot concatenate values of numeric data types, such as int and float, with strings.

Here is a program that shows how declarations and assignment statements could be arranged with respect to one another.

image

Output

Have you ever asked someone, “Do you know what time it is?” only to have the person smile smugly, say, “Yes, I do,” and walk away? This situation is like the one that currently exists between you and the computer. You now know enough C++ syntax to tell the computer to assign values to variables and to concatenate strings, but the computer won’t give you the results until you tell it to write them out.

In C++, we write out the values of variables and expressions by using a special variable named cout (pronounced “see-out”) along with the insertion operator (<<):

cout << "Hello";

This statement displays the characters Hello on the standard output device, usually the display screen.

The variable cout is predefined in C++ systems to denote an output stream. You can think of an output stream variable as a doorway through which we send a sequence of characters to an output device.

The insertion operator << (often pronounced as “put to”) takes two operands. Its left-hand operand is a stream expression (in the simplest case, just a stream variable such as cout). Its right-hand operand is an expression, as in the following two examples:

image

The insertion operator converts its right-hand operand to a sequence of characters and sends them to the output device through the stream variable.2 Notice how the << operator points in the direction the data is going—from the expression written on the right to the output stream on the left.

You can use the << operator several times in a single output statement. Each occurrence appends the next data item to the output stream. For example, we can write the preceding two output statements as

image

If bookTitle contains “American History”, both versions produce the same output:

The title is American History, 6th Edition

The output statement has the following form:

OutputStatement

image

The following output statements yield the output shown. These examples assume that the char variable ch contains the value '2', the string variable firstName contains "Marie", and the string variable lastName contains "Curie".

image

An output statement prints literal strings exactly as they appear. To let the computer know that you want to print a literal string—and not a named constant or variable—you must remember to use double quotes to enclose the string. If you don’t put quotes around a string, you’ll probably get an error message (such as UNDECLARED IDENTIFIER) from the C++ compiler. If you want to print a string that includes a double quote, you must type a backslash (\) character and a double quote, with no space between them, in the string. For example, to print the characters

Al "Butch" Jones

the output statement would look like this:

cout << "Al \"Butch\" Jones";

To conclude this introductory look at C++ output, we should mention how to terminate an output line. Normally, successive output statements cause the output to continue along the same line of the display screen. For example, the sequence

image

writes the following to the screen, all on the same line:

Hithere

To print the two words on separate lines, we can write this:

image

The output from these statements is

Hi
there

The identifier endl (meaning “end line”) is a special C++ feature called a manipulator. We discuss manipulators in detail in Chapter 3. For now, the important thing to note is that endl lets you finish an output line and go on to the next line whenever you wish.

Here is a program that contains declarations, assignment statements, and output statements.

image

image

Beyond Minimalism: Adding Comments to a Program

All you need to create a working program is the correct combination of declarations and executable statements. The compiler ignores comments, but they are of enormous help to anyone who must read the program. Comments can appear anywhere in a program except in the middle of an identifier, a reserved word, or a literal constant.

C++ comments come in two forms. The first is any sequence of characters enclosed by the /* */ pair. The compiler ignores anything within the pair. Here’s an example:

image

The second, and more common, form begins with two slashes (//) and extends to the end of that line of the program:

image

The compiler ignores anything after the two slashes.

Writing fully commented programs is good programming style. A comment should appear at the beginning of a program to explain what the program does:

image

Another good place for comments is in constant and variable declarations, where the comments explain how each identifier is used. In addition, comments should introduce each major step in a long program and should explain anything that is unusual or difficult to read (for example, a lengthy formula).

It is important to make your comments concise and to arrange them in the program so that they are easy to see and it is clear what they refer to. If comments are too long or crowd the statements in the program, they make the program more difficult to read—just the opposite of what you intended!

QUICK CHECK

images

2.1.1 What is the name of the one function that every C++ program must have? (p. 44)

2.1.2 What is the purpose of a metalanguage? (p. 47)

2.1.3 What is a data type? (p. 50)

2.1.4 Use the following syntax template to decide if the string “C++ 6th edition” is a valid “Sentence.” (pp. 47–48)

image

2.1.5 Which of the following are valid C++ identifiers? (pp. 49–50)

image

2.1.6 The only difference between a variable declaration and a constant declaration is that the reserved word const precedes the declaration. True or false? (pp. 55–56)

2.1.7 The reserved words listed in Appendix A cannot be used as identifiers. True or false? (p. 49)

2.1.8 Write a statement to assign the letter “A” to the char variable initial. (pp. 57–60)

2.1.9 What value is stored in the variable name by the following statement? (pp. 57–60)

image

2.1.10 Write a statement that sends the value in variable name to the stream cout. (pp. 60–62)

2.1.11 What is printed by the following statement, given the value assigned to name in Exercise 2.1.10? (pp. 60–62)

image

2.1.12 What are the two ways to write comments in C++? (pp. 62–63)

2.2 Program Construction

We have looked at basic elements of C++ programs: identifiers, declarations, variables, constants, expressions, statements, and comments. Now let’s see how to collect these elements into a program. As you saw earlier, C++ programs are made up of functions, one of which must be named main. A program also can have declarations that lie outside of any function. The syntax template for a program looks like this:

Program

image

A function definition consists of the function heading and its body, which is delimited by left and right braces:

FunctionDefinition

image

Here’s an example of a program with just one function, the main function:

image

The program begins with a comment that explains what the program does. Immediately after the comment, the following lines appear:

image

The #include lines instruct the C++ system to insert into our program the contents of the files named iostream and string. The first file contains information that C++ needs to output values to a stream such as cout. The second file contains information about the programmer-defined data type string. We discuss the purpose of these #include lines and the using statement a little later in the chapter.

Next comes a declaration section in which we define the constants FIRST, LAST, and MIDDLE.3 Comments explain how each identifier is used. The rest of the program is the function definition for our main function. The first line is the function heading: the reserved word int, the name of the function, and then opening and closing parentheses. (The parentheses inform the compiler that main is the name of a function, and not a variable or named constant.) The body of the function includes the declarations of two variables, firstLast and lastFirst, followed by a list of executable statements. The compiler translates these executable statements into machine language instructions. During the execution phase of the program, these instructions will be executed.

Our main function finishes by returning 0 as the function value:

return 0;

Recall that main returns an integer value to the operating system when it completes execution. This integer value is called the exit status. On most computer systems, you return an exit status of 0 to indicate successful completion of the program; otherwise, you return a nonzero value. Here is the output from the program.

image

Notice how we use spacing in the PrintName program to make it easy for someone to read. We use blank lines to separate statements into related groups, and we indent the entire body of the main function. The compiler doesn’t require us to format the program this way; we do so only to make it more readable. We will have more to say about formatting a program in Chapter 3.

Blocks (Compound Statements)

The body of a function is an example of a block (or compound statement). This is the syntax template for a block:

Block

image

A block is just a sequence of zero or more statements enclosed (delimited) by a { } pair. Now we can redefine a function definition as a heading followed by a block:

FunctionDefinition

image

In later chapters, when we learn how to write functions other than main, we will define the syntax of the heading in detail. In the case of the main function, the heading is simply

int main()

Here is the syntax template for a statement, limited to the C++ statements discussed in this chapter:

Statement

image

A statement can be empty (the null statement). The null statement is just a semicolon (;) and looks like this:

;

It does absolutely nothing at execution time; execution just proceeds to the next statement. The null statement is not used often.

As the syntax template shows, a statement can also be a declaration, an executable statement, or even a block. The last term means that you can use an entire block wherever a single statement is allowed. In later chapters in which we introduce the syntax for branching and looping structures, this fact is very important.

We use blocks often, especially as parts of other statements. Leaving out a { } pair can dramatically change the meaning as well as the execution of a program. This is why we always indent the statements inside a block—the indentation makes a block easy to spot in a long, complicated program.

Notice in the syntax templates for the block and the statement that there is no mention of semicolons—yet the PrintName program contains many semicolons. If you look back at the templates for constant declaration, variable declaration, assignment statement, and output statement, you can see that a semicolon is required at the end of each kind of statement. However, the syntax template for the block shows no semicolon after the right brace. The rule for using semicolons in C++, then, is quite simple: Terminate each statement except a compound statement (block) with a semicolon.

One more thing about blocks and statements: According to the syntax template for a statement, a declaration is officially considered to be a statement. A declaration, therefore, can appear wherever an executable statement can. In a block, we can mix declarations and executable statements if we wish:

image

We prefer, however, to group the declarations together before the start of the executable statements:

image

The C++ Preprocessor

Imagine that you are the C++ compiler. You are presented with the following program. You are to check it for syntax errors and, if there are no syntax errors, you are to translate it into machine language code.

image

You, the compiler, recognize the identifier int as a C++ reserved word and the identifier main as the name of a required function. But what about the identifiers cout and endl? The programmer has not declared them as variables or named constants, and they are not reserved words. You have no choice but to issue an error message and give up.

To fix this program, the first thing we must do is insert a line near the top that says

image

just as we did in the PrintName program (as well as in the sample program at the beginning of this chapter and the LeapYear program of Chapter 1). This line says to insert the contents of a file named iostream into the program. This file contains declarations of cout, endl, and other items needed to perform stream input and output. The #include line is not handled by the C++ compiler, but rather by a program known as the preprocessor.

The preprocessor concept is fundamental to C++. The preprocessor is a program that acts as a filter during the compilation phase. Your source program passes through the preprocessor on its way to the compiler (see FIGURE 2.2).

image

FIGURE 2.2 C++ preprocessor

A line beginning with a pound sign (#) is not considered to be a C++ language statement (and thus is not terminated by a semicolon). Instead, it is called a preprocessor directive. The preprocessor expands an #include directive by physically inserting the contents of the named file into your source program. A file whose name appears in an #include directive is called a header file. Header files contain constant, variable, data type, and function declarations needed by a program.

In the directives

image

the angle brackets < > are required. They tell the preprocessor to look for the files in the standard include directory—which contains all the header files that are related to the C++ standard library. The file iostream contains declarations of input/output facilities, and the file string contains declarations for the string data type. In Chapter 3, we make use of header files other than iostream and string.4

Using the std Namespace

In our HappyBirthday program, even if we add the preprocessor directive #include <iostream>, the program will not compile. The compiler still doesn’t recognize the identifiers cout and endl. The problem is that the header file iostream (and, in fact, every standard header file) declares all of its identifiers to be in a namespace block called std:

image

Don’t worry about writing namespace block declarations for now; the important point is that an identifier declared within a namespace block can be accessed directly only by statements within that same block. To access an identifier that is “hidden” inside a namespace, the programmer has several options. We describe the simplest one here. Chapter 8 describes namespaces in more detail.

If we insert a statement called a using directive

using namespace std;

near the top of the program before the main function, then we make all the identifiers in the std namespace accessible to our program.

image

This option is the one we used in the PrintName program and in sample programs earlier in the chapter. In many of the following chapters, we continue to use this method. However, in Chapter 8 we discuss why the approach is not advisable in large programs.

SOFTWARE MAINTENANCE CASE STUDY: Adding Titles to Names

In Chapter 1, we saw the essential steps of any software maintenance process:

1. Check the operation of the existing code.

2. Understand sufficiently how the code works to do the maintenance task.

3. Create a working copy of the code.

4. Make the modifications.

5. Test the new code.

6. Clean up any inconsistencies and update any related documentation.

For the HelloWorld program in Chapter 1, the code was so trivial that we could understand its operation and make the changes without any knowledge of C++ syntax. In the following scenario, the code is not so simple. We’ll need to study it more consciously to discover how it works and what needs to be changed.

MAINTENANCE TASK: Add the title “Mr.” to each of the name formats in the PrintName application.

EXISTING CODE

image

CODE ANALYSIS: To understand any sizable piece of code, we look for recognizable chunks into which we can partition it. One of our goals is to set aside portions that are unlikely to be related to our maintenance task. In this case, we see that there are opening comments:

image

The next chunk is made up of the include and using statements. These also are unrelated to the maintenance task.

image

Next comes the declaration of the three constants:

image

Because the heading of main simply follows the standard formula, we can also set aside that line of code and its associated braces. Thus the portion of this application that we must focus on is the block within main. That block can be further partitioned into a section of declarations:

image

There is also a section that generates and prints each of the two name formats:

image

Thus the major steps in the application are declarations and output.

SOLUTION: Our task is to modify the application to incorporate a title in each of the formats. How are we to do this? We must begin with the problem-solving technique of asking questions.

How do we get the title? We need to declare a string constant TITLE that contains the string "Mr.".

image

How is the title to be incorporated into the formats? For the first format, it simply appears at the beginning of the name. For the second format, it appears after the last name. Now we must decide how to accomplish this within the application. There are two places where we could add the title to each format. One place is within the assignment statements that generate the format. For example:

image

The other place is within the output statements that print the names:

image

With the second format, the change is not so simple, because the title must be inserted between the names. We could still change the output statement, but it would not make use of the contents of lastFirst, which would be wasteful.

In this short application, it doesn’t really matter which approach we choose. But in a larger application, we would need to check whether the firstLast and lastFirst variables are used elsewhere. If they are, then we must understand how they are used. In this case, however, let’s simply insert the title into the assignment statements.

REVISED CODE: Here is the revised application, with the inserted or modified code highlighted.

image

As you can see, the actual changes are quite small. By analyzing the original code and breaking it into meaningful sections, we could isolate the steps that required modification. It was then obvious how and where to make each of the changes. Here is the output:

image

Software Maintenance Tips

1. Break a long block of code into smaller chunks that have distinct purposes.

2. Identify portions of the code that you are sure you can ignore.

3. Focus on those code sections that are clearly related to the maintenance task.

4. Make sure that you understand which changes are required. Ask questions about anything that is unclear. Formally, this information would be written as a specification document.

5. Consider the major steps that you’ve identified in the existing code. Then establish how you would solve the maintenance task within that overall approach. For example, in a simple application whose steps are input, process, and output, think about how the task relates to each of these steps. It doesn’t help to develop a solution for a maintenance task that takes a completely different approach from the existing code. You must work within the given context.

6. Look for how your changes affect other parts of the application. If you’re changing the content of an existing variable, check whether it is used elsewhere and how it is used.

7. Document your changes. In our example, we highlighted the updated code. Some companies have standards for how code changes are recorded. For example, each line may have a comment that identifies the programmer and the date of the change. There are also code management tools that automatically record this information.

QUICK CHECK

images

2.2.1 What is an exit status? (p. 66)

2.2.2 What value is given to the exit status on most computing systems when a program completes successfully? (p. 66)

2.2.3 What is a block (compound statement)? (p. 66)

2.2.4 What is an example of a block in this section? (p. 66)

2.2.5 What are the five statements discussed in this section? (p. 67)

2.2.6 What character is used to terminate a statement? (p. 67)

2.2.7 Which statement does not need to be terminated? (p. 67)

2.2.8 Which compilation phase expands #include<iostream>? (p. 68)

2.2.9 What is the #include called? (p. 69)

2.2.10 Every standard header file declares all of its identifiers to be in a “hidden” place called std. What is the general term used for std? (p. 69)

2.2.11 Fill in the blanks in the following statements, which appear at the beginning of a program. (pp. 70–72)

image

2.3 More About Output

We can control both the horizontal and vertical spacing of our output to make it more appealing (and understandable). Let’s look first at vertical spacing.

Creating Blank Lines

We control vertical spacing by using the endl manipulator in an output statement. You have seen that a sequence of output statements continues to write characters across the current line until an endl terminates the line. Here are some examples:

image

What do you think the following statements print out?

image

The first output statement causes the words Hi there, to be printed; the endl causes the screen cursor to go to the next line. The next statement prints nothing but goes on to the next line. The third statement prints the words Lois Lane. and terminates the line. The resulting output is the three lines

Hi there,

Lois Lane.

Whenever you use an endl immediately after another endl, a blank line is produced. As you might guess, three consecutive uses of endl produce two blank lines, four consecutive uses produce three blank lines, and so forth.

Note that we have a great deal of flexibility in how we write an output statement in a C++ program. We could combine the three preceding statements into two statements:

image

In fact, we could do it all in one statement. One possibility is this:

image

Here’s another:

image

The last example shows that you can spread a single C++ statement onto more than one line of the program. The compiler treats the semicolon—not the physical end of a line—as the end of a statement.

Inserting Blanks Within a Line

To control the horizontal spacing of the output, one technique is to send extra blank characters to the output stream. (Remember that the blank character, which is generated by pressing the spacebar on a keyboard, is a perfectly valid character in C++.)

For example, to produce this output:

image

you would use these statements:

image

All of the blanks and asterisks are enclosed in double quotes, so they print literally as they are written in the program. The extra endl manipulators give you the blank lines between the rows of asterisks.

If you want blanks to be printed, you must enclose them in quotes. The statement

image

produces the following output:

**

Despite all of the blanks we included in the output statement, the asterisks print side by side because the blanks are not enclosed by quotes.

Special Characters

We have already mentioned that to include a double-quote mark within a string, it must be preceded by a backslash (\"). C++ defines some additional special characters using the backslash prefix. Here we discuss three that are particularly useful.

We use the endl manipulator in an output stream to go to the next line. You can also use the newline character \n to place a newline operation directly in a string. The following two output statements both produce the same output:

image

They each display:

The answer is:
42

The second statement is clearly more readable, of course. Starting with Chapter 8, we’ll encounter cases where embedding \n in a string makes sense. For now, we recommend always using endl.

If you want to include a single quote mark as a char literal within an output stream, then it must also be preceded by a backslash: '\''. Note that there is no problem including single quote marks in strings or writing a double quote directly as a char literal. For example, to output a double quote followed by a single quote, we can write either:

image

or

image

Given that the backslash has these special uses, it is natural to wonder how a backslash would be output. The answer is that we must write two backslashes in a row:

image

QUICK CHECK

images

2.3.1 What is used to control vertical spacing in an output statement? (p. 74)

2.3.2 What is the special character for newline? (p. 76)

2.4 Program Entry, Correction, and Execution

In this section, we examine the program entry process in general. You should consult the manual for your specific computer to learn the details.

Entering a Program

The first step in entering a program is to get the computer’s attention. With a personal computer, you just turn it on or wake it from sleep mode. If you’re using a computer in an educational lab, you may need to log on by entering a user name and password.

Once the computer is ready to accept your commands, you tell it that you want to enter a program by having it run a development environment, which includes a code editor. The code editor is a program that allows you to create and modify the code for a program, which is then stored on a file.

File A named area in secondary storage that is used to hold a collection of data; the collection of data itself.

A file is a collection of data that has a name associated with it. You usually choose the name for the file when you create it with the editor. From that point on, you refer to the file by the name you’ve given it.

There are so many different types of editors, each with different features, that we can’t begin to describe them all here. But we can describe some of their general characteristics.

A code editor is very much like a word processor. It allows you to type in a window and move around the file with your mouse and keyboard to make changes. In most development environments, the code editor also color-codes different parts of C++ syntax to make it easier to more distinctly see keywords, variables, comments, and so on. It will also help you to automatically indent lines of code. Some code editors check syntax as you type, flagging parts of your code that may not compile.

In many development environments, the compiler will automatically use the code editor to display the location where it detects an error in your code. Note, however, that a compiler sometimes detects an error in a place much different from where it actually occurred. For example, the compiler will point out the first place that an undeclared variable name is used, when the source of the error may be that you forgot a using statement.

Compiling and Running a Program

Once your program is stored in a file, you compile it by issuing a command to run the C++ compiler. The compiler translates the program, then stores the machine language version into a file. The compiler may display messages indicating errors in the program, either in a separate window or in the editor.

If the compiler finds errors in your program (syntax errors), you have to determine their cause, fix them in the editor, and then run the compiler again. Once your program compiles without errors, you can run (execute) it.

Some systems automatically run a program when it compiles successfully. On other systems, you have to issue a separate command to run the program. Still other systems require that you specify an extra step called linking between compiling and running a program. Whatever series of commands your system uses, the result is the same: Your program is loaded into memory and executed by the computer.

Even though a program runs, it may still have errors in its design. The computer does exactly what you tell it to do, even if that’s not what you wanted it to do. If your program doesn’t do what it should (a logic error), you have to return to the algorithm and fix it, and then go back to the editor and fix the program. Finally, you compile and run the program again. This debugging process is repeated until the program does what it is supposed to do (see FIGURE 2.3).

QUICK CHECK

images

2.4.1 What does a compiler translate a program into? (p. 77)

2.4.2 What do we call an error that occurs when the program is executing? (p. 77)

image

FIGURE 2.3 Debugging Process

Problem-Solving Case Study

Printing a Chessboard

PROBLEM: Your college is hosting a chess tournament, and the people running the tournament want to record the final positions of the pieces in each game on a sheet of paper with a chessboard preprinted on it. Your job is to write a program to preprint these pieces of paper. The chessboard is an eight-by-eight pattern of squares that alternate between black and white, with the upper-left square being white. You need to print out squares of light characters (spaces) and dark characters (such as *) in this pattern to form the chessboard.

DISCUSSION: You could simply type up a program consisting of a series of output statements with alternating patterns of blanks and asterisks. But the organizers of the tournament aren’t sure exactly what they want the chessboard to look like, and you decide it would be safer to write the program in a manner that allows the design of the chessboard to be modified easily.

It is easy to make a changeable chessboard if the design is built up using string variables. You can begin by defining string constants for rows of characters that make up the black and white areas. Then you can concatenate these constants to form variables containing the alternating patterns of black and white that are repeated to print a row of chessboard squares. For example, suppose your initial design for the chessboard looks like this:

image

You can begin by defining constants for WHITE and BLACK that contain eight blanks and eight asterisks respectively, and then concatenate these together into variables that follow the patterns

WHITE + BLACK + WHITE + BLACK + WHITE + BLACK + WHITE + BLACK

and

BLACK + WHITE + BLACK + WHITE + BLACK + WHITE + BLACK + WHITE

These variables can then be printed with cout statements the appropriate number of times to create the chessboard.

From this discussion we know that there are two constants and two variables, as summarized in the following tables:

CONSTANTS

image

VARIABLES

image

If we look carefully at the chessboard, the algorithm jumps out at us. We need to output five whiteRows, five blackRows, five whiteRows, five blackRows, five whiteRows, five blackRows, five whiteRows, and five blackRows. We can summarize this in our algorithm to output the five whiteRows and five blackRows four times.

Repeat four times
Output five whiteRows
Output five blackRows

image

image

There are eight blocks of five lines that look alike. In Chapter 6, we introduce a looping statement that allows us to shorten this program considerably.

After you show the printout from this program to the organizers of the tournament, they suggest that the board would look better with the "#" character instead of "*" filling the black squares. You cheerfully agree to make this change because you know that the only difference in the program is that the value of the constant BLACK becomes "########".

Testing and Debugging

1. Every identifier that isn’t a C++ reserved word must be declared. If you use a name that hasn’t been declared—either by creating your own declaration statements or by including a header file—you get an error message.

2. If you try to declare an identifier that is the same as a reserved word in C++, you get an error message from the compiler. See Appendix A for a list of reserved words.

3. C++ is a case-sensitive language. Two identifiers that are capitalized differently are treated as two different identifiers. The word main and all C++ reserved words use only lowercase letters.

4. To use identifiers from the standard library, such as cout and string, you must put a using directive near the top of your program:

using namespace std;

5. Check for mismatched quotes in char and string literals. Each char literal begins and ends with an apostrophe (a single quote). Each string literal begins and ends with a double quote.

6. Be sure to use only the apostrophe (') to enclose char literals. Most keyboards also have a reverse apostrophe (´), which is easily confused with the apostrophe. If you use the reverse apostrophe, the compiler will issue an error message.

7. To use a double quote within a literal string, use the two symbols \" in a row. If you use just a double quote, it ends the string, and the compiler then sees the remainder of the string as an error.

8. In an assignment statement, be sure that the identifier to the left of = is a variable and not a named constant.

9. In assigning a value to a string variable, the expression to the right of = must be a string expression, a string literal, or a char.

10. In a concatenation expression, at least one of the two operands of + must be of type string. For example, the operands cannot both be literal strings or char values.6

11. Make sure your statements end in semicolons (except blocks, which do not have a semicolon after the right brace).

image Summary

The syntax (grammar) of the C++ language is defined by a metalanguage. In this text, we use a form of metalanguage called syntax templates. We describe the semantics (meaning) of C++ statements in English.

Identifiers are used in C++ to name things. Some identifiers, called reserved words, have predefined meanings in the language; others are created by the programmer. The identifiers you invent are restricted to those not reserved by the C++ language. Reserved words are listed in Appendix A.

Identifiers are associated with memory locations by declarations. A declaration may give a name to a location whose value does not change (a constant) or to one whose value can change (a variable). Every constant and variable has an associated data type. C++ provides many built-in data types; the most commonly used are int, float, and char. Additionally, C++ permits programmer-defined types, such as the string type from the standard library.

The assignment operator is used to change the value of a variable by assigning it the value of an expression. At execution time, the expression is evaluated and the result is stored into the variable. With the string type, the plus sign (+) is an operator that concatenates two strings. A string expression can concatenate any number of strings to form a new string value.

Program output is accomplished by means of the output stream variable cout, along with the insertion operator (<<). Each insertion operation sends output data to the standard output device. When an endl manipulator appears instead of a data item, the computer terminates the current output line and goes on to the next line.

Output should be clear, understandable, and neatly arranged. Messages in the output should describe the significance of values. Blank lines (produced by successive uses of the endl manipulator) and blank spaces within lines help to organize the output and improve its appearance.

A C++ program is a collection of one or more function definitions (and optionally some declarations outside of any function). One of the functions must be named main. Execution of a program always begins with the main function. Collectively, the functions all cooperate to produce the desired results.

image Quick Check Answers

2.1.1 main 2.1.2 To provide a language for expressing the syntax rules of a programming language. 2.1.3 A specific set of values, together with a set of operations that can be applied to those values. 2.1.4 No. According to the template, a Sentence must end in a period, exclamation point, or question mark. 2.1.5 Hello, Bob, maximum, and all_4_one are the valid C++ identifiers. 2.1.6 False. The constant declaration also assigns a literal value to the identifier. 2.1.7 True. Identifiers must differ from reserved words. 2.1.8 initial = 'A'; 2.1.9 Alexander Q. Smith 2.1.10 cout << name; 2.1.11 The string Alexander Q. Smith Jr. is output and then successive output will appear on the next line. 2.1.12 Between the delimiters /* and */, or following // to the end of the line. 2.2.1 The exit status is an integer value that is returned from the main function to the operating system when it completes execution. 2.2.2 A 0 value indicates success. 2.2.3 A block is a sequence of zero or more statements enclosed by a { } pair. 2.2.4 The body of a function. 2.2.5 Null, Declaration, Assignment, Output, and Block. 2.2.6 A semicolon (;) 2.2.7 Block 2.2.8 Preprocessor phase. 2.2.9 A preprocessor directive. 2.2.10 A namespace.

2.2.11 #include <iostream>
           #include <string>
           using namespace std;

2.3.1 endl 2.3.2 '\n' 2.4.1 Machine language. 2.4.2 A logic error or runtime error.

image Exam Preparation Exercises

1. Mark the following identifiers as either valid or invalid.

image

2. Match the following terms with the definitions given below.

a.  Function

b.  Syntax

c.  Semantics

d.  Metalanguage

e.  Identifier

f.  Data type

g.  Variable

h.  Named constant

i.  Literal

j.  Expression

i. An identifier referring to a value that can be changed.

ii.  A specific set of values, along with operations that can be applied to those values.

iii.  A value that appears in a program.

iv. The set of rules that determines the meaning of instructions written in a programming language.

v. A language that is used to write the rules for another language.

vi. A subprogram in C++.

vii. A name that is used to refer to a function or data object.

viii. An arrangement of identifiers, literals, and operators that can be evaluated.

ix. The formal rules governing how valid instructions are written in a programming language.

x. An identifier referring to a value that cannot be changed.

3. A reserved word is a named constant that is predefined in C++. True or false?

4. What is wrong with the following syntax template for a C++ identifier?

image

5. A char literal can be enclosed either in single or double quotes. True or false?

6. The null string represents a string containing no characters. True or false?

7. Concatenation works with a char value only when its other operand is a string value. True or false?

8. What is output by the following statement?

image

9. Given these assignments to string variables:

image

What is the value of the following expression?

image

10. How do we insert a double quote into a string?

11. What danger is associated with the use of the /* and */ form of comment, and is avoided by use of the // comment form?

12. What are the limitations associated with the // form of comment?

13. What is the name of the << operator, and how might we pronounce it in reading a line of code that contains it?

14. Can we concatenate endl with a string in an expression? Explain.

15. What does the #include preprocessor directive do?

16. For the following output statement, the compiler reports this error message: UNDECLARED IDENTIFIER: cout.

image

The program includes the statement

using namespace std;

What is the most likely source of this error?

17. What is the name of the C++ construct that begins with { and ends with }?

18. How do you write a string that is too long to fit on a single line?

19. Reorder the following lines to make a working program.

image

image Programming Warm-Up Exercises

1. Write an output statement that prints today’s date and then goes to the next line.

2. Write a single output statement that outputs the following three lines:

image

3. Write declarations for the following:

a.  A named constant, called ANSWER, of type string, with the value "True"

b.  A char variable with the name middleInitial

c.  A string variable with the name courseTitle

d.  A named char constant, called PERCENT, with the value '%'

4. Change the three declarations in the PrintName program on page 65 so that it prints your name.

5. Given the following declarations:

image

Write an output statement that prints the title of this book, using only the cout stream, the stream insertion operator, and the above-named constants.

6. Write an expression that results in a string containing the title of this book, using only the concatenation operator and the declarations of Exercise 5.

7. Write C++ output statements to print the following exactly as shown (a portion of the text found in the preface of this book, relating the action of a weaver’s loom to the complexity of human thought). You should write one output statement for each line of text.

image

8. Fill in the missing lines in the following program, which outputs:

image

9. Enter and run the following program. Be sure to type it exactly as it appears here, but substitute your name and the date as indicated.

image

10. Find the 10 missing items in the following program.

image

image

image Programming Problems

1. Write a program that prints out your course schedule for a single week. Here’s an example of the output for one day:

image

Use named string constants wherever possible to avoid retyping any words or numbers. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do with the programs in this chapter.

2. Write a program that prints out all six permutations of the ordering of the following three lines. Declare a named constant for each line and write an output statement for each permutation. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do with the programs in this chapter.

image

3. Write a program that displays a checkerboard pattern made of stars and blanks, as shown below. A checkerboard is eight squares by eight squares. This will be easier if you first declare two named string constants representing the two different row patterns. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do with the programs in this chapter.

image

4. Write a program that prints out business cards for yourself. A card should include your name, street address, phone number(s), and email address. You also can make up a company name and put that on the card if you wish. To save paper, the program should print eight cards per page, arranged in two columns of four cards. To reduce typing, you should declare a named string constant for each line of the card, and then write output statements to print the eight cards using those constants. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do with the programs in this chapter.

image Case Study Follow-Up

1. What change would you have to make to the Chessboard program to cause the black squares to be printed with the "%" character instead of "*"?

2. How would you change the Chessboard program to print periods in the white squares instead of blanks?

3. How would you change the Chessboard program if you wanted to reverse the colors (that is, make black squares white, and vice versa) without changing the constant declarations or the values of whiteRow and blackRow?

4. Change the Chessboard program so that the squares are 10 characters wide by 8 rows high.

5. The organizers of the chess tournament find it difficult to write in the black squares of the printed chessboard. They want you to change the program so that the black squares have a blank space in their center that is four characters wide and two lines high. (Hint: You may have to define another string constant.)

6. How many characters are stored in each of the string variables in the Chessboard program?

1. As noted in Chapter 1, most programming languages use the ASCII character code. C++ provides the data type wchar_t (for “wide character”) to accommodate larger character sets such as Unicode. In C++, the notation L'something' denotes a value of type wchar_t, where the something depends on the particular wide character set being used. We do not examine wide characters any further in this book.

2. In C++ terminology, it is said that the stream expression is “appended to” the output stream.

3. As with our earlier demonstration programs, we could have declared these constants within main. Here we place them before main as an example of what are called global declarations. We return to this topic in a later chapter.

4. In the C language and in prestandard C++, the standard header files end in the suffix .h (for example, iostream.h), where the h suggests “header file.” In ISO/ANSI C++, the standard header files no longer use the .h suffix.

5. The output lines are shown next to the output statement that ends each of them. There are no blank lines in the actual output from these statements.

6. The invalid concatenation expression "Hi" + "there" results in a confusing syntax error message such as INVALID POINTER ADDITION. For now, just keep in mind that such a message may indicate a concatenation expression that doesn’t include a variable.