Section 7  - A Console Example

2A.7.1 cout

In the Hello World! program we saw the statement

cout << "Hello World\n";

which sent to the screen the words inside the quotation marks. There is a name for these words inside quotation marks.  They are called strings.

String Terminology

A character string, or just string, is a natural language sequence. It is a series of letters, numerals or special symbols like "This is the ** 3rd ** best day of my life!!!".  Sometimes we refer to constructs like this as string literals to distinguish them from string variables which we will talk about a little later.  For now, you can use the term string or string literal to refer to anything inside double quotes "like this".

We will now consider a more elaborate form of the cout statement.

First let's look at an entire program:

#include <iostream>
using namespace std;

int main()
{
int someNumber;

someNumber = -7;
someNumber = (someNumber - 2) / 3;
cout << "The final value of someNumber: " << someNumber << endl;

// just some more stuff to demo / and %
cout << "\n-7 divided by 2 is " << (-7 / 2) << endl;
cout << "\n-7 mod 2 is " << (-7 % 2) << endl;

return (0);
}

The output of this program is:

The final value of someNumber: -3

-7 divided by 2 is -3

-7 mod 2 is -1

I am hearing several questions:

QUESTION 1)  In the Hello World! program the cout statement was easy. But here there are several occurrences of the << symbol on each line, and some items on the line are in quotes, while others are not.  Also a '\n' is inside the quotes. What is all this stuff?!

ANSWER) Let's look at the first cout statement:

cout << "The final value of someNumber: " << someNumber << endl;

Inside a cout statement, you can use the << operator to glue together several  items as you see above.    If this results in a long line you can always spread the statement over two or more lines.  Recall that we already learned that we can place a long statement on several lines because C++ doesn't care about whitespace

scenic grapes

Second, you mentioned that some items are in quotes while others are not. As we just learned, the items in quotes are character string literals, or simply strings and are printed out exactly as you see them.  The items not in quotes, like the object someNumber, are numeric variables, and they are  first replaced with their values before they are  printed.  So if someNumber is not in quotes,  you would not see the word "someNumber" printed, but rather the value that it contains,

A '\n'  can be put in anywhere inside a string literal, and it will result in a newline at that location in the string.

QUESTION 2)  Okay, so you can output the value of an object by putting it outside the quotes in the output stream. But what's going on in the second cout statement? I see -7/2 where I expect to see an object name or a literal string.

ANSWER) In addition to single variable object identifiers, we can write entire numeric expressions right there in the output stream. This is done if we want to compute a value and send it  to the screen immediately without storing the result in some variable.

This expression could be made up of constants like

-7/2

or a combination of constants and variables like

(someNumber + 3) / (someNumber - 3)

Even though you can do this, don't do it too often. It is better to waste variables on intermediate results because it makes debugging the program easier. If you put too much computation unnecessarily into your output statements, I will take off points.

Don't lose points:

In fact, I will take off points if you EVER do a computation in the cout statement.

 

I will not take off a point if you first store the value into a variable, then output that variable.

QUESTION 3) What does the % mean in the expression -7 % 2?

ANSWER) The % is an operator, like +, -, *, and /. It's called the modulo operator. This operator computes the remainder of the left number divided by the right number, i.e. ,

27 % 4  equals 3
-3 % 2  equals -1
5 % 10 equals 5

We read this "27 mod 4."

The modulo operator is very useful in all kinds of non-mathematical applications. You'll see.

Notes:

  1. You can use either a '\n', inside quotes, or an endl, outside quotes, to create a newline during output. The difference is a matter of convenience (although hair-splitters might be interested in knowing that endl flushes the output stream while '\n' does not - a technicality that I don't expect most of you to understand at this stage).
  2. If a string literal is long, don't try to spread it over multiple lines unless you first break it up into smaller Strings.  Do it like this:
    cout << "Now is the time "
    << "for all good students to "
    << "come to the aid of their "
    << "community college.";
    

    Also note, that the above example will print the entire sentence on one line.  (Why?)