Chapter 5 Cout Statement

SECTION 5.1  Basic Cout Statement

One of the first things to learn in C++ is how to print text to the screen.  This may be done using the cout (console out) statement, which is used to output text, variables, and mathematical expressions to the screen.  This statement is located in the iostream.h library which must be included at the top of your program.  The cout statement is followed by << then the text to output.  All cout statements should be terminated with a semi-colon.

Program 5.1 - Basic Cout Statement

#include <iostream>
using namespace std;
main()
{
  cout << "Hello World! ";
  cout << "This is one my first programs.";
}

Output
Hello World! This is one of my first programs.

SECTION 5.2  Printing a Newline

You may have noticed that the two sentences in the previous program printed on the same line.  Just because you put a second cout statement on the next line does not cause your output to skip to the next line.  The following programs show the two ways to print a newline after the first sentence.  Both programs output the same thing.

Program 5.2 Two Ways to Print a Newline

#include <iostream>
using namespace std;
main()
{
  cout << "Hello World!\n";
  cout << "This is one my first programs.";
}

#include <iostream.h>
using namespace std;
main()
{
  cout << "Hello World!";
  cout << endl;
  cout << "This is one my first programs.";
}

Output
Hello World!
This is one of my first programs.

The first program used a "\n" to skip to a new line.  The second program uses an endl (endline) to skip to the next line.  The cout statement below shows how to combine both lines using a \n in between.

cout << "Hello World!\nThis is one of my first programs";

Important to Remember: The \n must be within double quotes whereas the endl does not.

 SECTION 5.3  Printing Variables

In addition to printing text, you may use cout statements to output variables.  Variables are covered in more detail in chapter 6.  In the following program, two integer variables (A and B) are declared and assigned values of 2 and 3, respectively.

Program 5.3 - Printing Variables

#include <iostream>
using namespace std;
main()
{
  int A = 2;
  int B = 3;
  cout << "I will now print the values of A and B.\n";
  cout << A;
  cout << endl;
  cout << B;
  cout << endl;
}

Output
I will now print the values of A and B.
2
3


 

SECTION 5.4  Printing Mathematical Expressions

 

The cout statement is also used for printing mathematical expressions.  Numbers and/or variable values may be added, subtracted, multiplied, and divided as shown in the next program.

 

Program 5.4a Printing Mathematical Expressions

#include <iostream>
using namespace std;
main()
{
  cout << 5 + 4 << endl;
  cout << 5 * 4 << endl;
  cout << 9 / 3 << endl;
  cout << 9 3 << endl;
  cout << 8 % 5 << endl;
}

Output
9
20

3

6

3

 

There are 5 mathematical operators in C++: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).  The modulus operator (%) calculates the remainder in a division.  If you divide 20 by 3, you will get 6, with a remainder of 2.  The following cout statement will print 2:

cout << 20%3;

 

The next program listing uses variables in the mathematical expressions.

 

Program 5.4b - Printing Mathematical Expressions

#include <iostream>
using namespace std;

main()
{
  int A = 2;
  int B = 3;
  cout << "The product of A and B is ";
  cout << A * B;
  cout << endl;
  cout << "A + B * 4 / 2 is equal to ";
  cout << A + B * 4 / 2;
  cout << endl;
}

Output
The product of A and B is 6
A + B * 4 / 2 is equal to 8

Order of Operations - In the last mathematical expression in the above program, you do the multiplication and division before you do the addition.  Therefore, A + B * 4 / 2 is equal to 8.  Multiplication and division are calculated (from left to right) before addition and subtraction.  Items in parenthesis are calculated first, however.  Therefore, 8 / 4 + 2 * 3 * (1 + 3) is equal to 26.
 

SECTION  5.5  Extending the Cout Statement

Every time you switch between printing variables, text (within quotes), mathematical expressions, or an endl, you either need to use another cout statement, or extend the cout statement by adding another <<.  The following program is the same as Program 5.3, but with the last cout statement extended.

Program 5.5a Extending Cout Statements

#include <iostream>
using namespace std;
main()
{
  int A = 2;
  int B = 3;
  cout << "I will now print the values of A and and B.\n";
  cout << A << endl << B << endl;
}

Output
I will now print the values of A and B.
2
3

The final program in this section declares two variables, prints text, mathematical expressions, and uses one cout statement extended and on multiple lines.  When you extend a cout statement to multiple lines, there is only one semicolon placed at the very end.

Program 5.5b Extending Cout to Multiple Lines

#include <iostream>
using namespace std;
main()
{
  int X = 4;
  int Y = 5;
  cout << "X * Y = " << X * Y << endl
       << "X + Y = " << X + Y << endl
       << "X - 2 * 3 = " << X - 2 * 3 << endl;
}

Output
X * Y = 20
X + Y = 9
X 2 * 3 = -2

 

SECTION  5.6  Comments and Documentation

 

Whenever you place two backslashes // on a line, any text that follows is ignored by the compiler it has no effect on the execution of the program.  Comments allow programmers to place information in a program to explain what is happening.  At the top of a program, you could add information on who the programmer is, what version the program is, etc.  It is good practice to add documentation to your program so that someone else looking at it will understand it.  Even if nobody else will ever look at your program, it is good to document the program for yourself it is easy to forget what a program does and how it works when you have not looked at it in a while.

 

Program 5.6 Comments

// This program multiplies two numbers.

// Author: David Kirk

 

#include <iostream>
using namespace std;
main()
{
  int A = 18;   
// This is the first number
  int B = 3;    
// This is the second number


  cout << "The product of " << A << " and " << B << " is " << A * B << endl;
}

Output
The product of 18 and 3 is 54

 

Using two backslashes // is the newew C++ style of comments.  The older C method of comments uses a /* to begin the comment, and a */ to end the comment.  This allows you to comment out a section of code, which is useful for debugging.  The following program demonstrates this.

 

Program 5.7 Alternative method for Comments

#include <iostream>
using namespace std;
main()
{

  cout << "Welcome!\n";

 

/*

  int A = 18;

  int B = 3;
  cout << "The product of " << A << " and " << B << " is " << A * B << endl;

*/

 

}

Output
Welcome!

 

SECTION 5.7  Backslash Special Characters

 

Within the double quotes " " of a cout statement, A backslash \ has a special function.  It is always followed by another character that tells what function it is.  You have already learned about printing a newline using "\n".  The following table lists other backslash functions.

 

Backslash Functions

Code Explanation

Example Cout Statement

Output

\n

Used to print a newline which skips to the next line.

cout << "A\nB\nC";

A
B
C

\t

Used to print a tab.

cout << "A\tB\tC";

A     B     C

\\

Used to print a backslash.  This must be used since the backslash is always followed by another character

cout << "c:\\Windows";

c:\Windows

\"

Used to print a double-quote.  This must be used since double-quotes are used as the container for all output.

cout << "She said \"hi\".";

She said "hi".

\e

Sends an escape character ASCII 27.  Often used in escape sequences in a telnet window to change text colors, cursor position, etc.  This is discussed further in the chapter titled "More Input/Output".

cout << "\e[2J";

<clears screen>

 

 

Exercises Chapter 5

 

What will each of the following output?

 

1.   cout << "Hello" << "World";

 

2.   int A = 1;

     int B = 3;

     cout << A << " is less than " << B;

 

3.   int X = 4;

     int Y = 2;

     cout << "X * Y = " << X * Y << endl << "The End";

 

4.   int Z = 1;

     cout << "Z";

 

5.   cout << "A" << "B" << endl << "C" << "D";

 

6.   cout << 5 + 6 * 3 2;

 

7.   cout << (17 + 3) / 4;

 

8.   int A = 15;

     cout << A % 4;

 

9.   int B = 50;

     cout << B * (3 1) + 20;

 

10.  cout << "3\t2\t1\tGO!";

 

11.  cout << "\"apples\"\nare good";

 

12.  cout << "/---\\\n| \" |\n\\ - /";