One of the first things to learn in C++ is how to print text to the screen (Windows Command Prompt or Mac Terminal). This may be done using the cout (console out) statement, which is used to output text, variables, and mathematical expressions to the screen. The cout function is located in the iostream.h library which must be included at the top of your program. The cout statement is usually followed by the insertion operator << to redirect the data that follows to output. All cout statements should be terminated with a semi-colon.
#include <iostream> using namespace std; main() { cout << "Hello World! "; cout << "This is one my first programs."; } |
Hello World! This is one of my first programs. |
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.
#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."; } |
Hello World! This is one of my first programs. |
The first program used a \n inside the quotations to skip to a new line. The second program uses an endl (endline) outside of the quotations 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";
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.
Code | Explanation | Example Usage | Output |
\n |
Print a newline which skips to the next line. |
cout << "A\nB\nC"; |
A |
\t |
Print a tab. |
cout << "A\tB\tC"; |
A B C |
\\ |
Print a backslash. This must be used since the backslash is always followed by another character. |
cout << "c:\\Windows"; |
c:\Windows |
\" |
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". |
In addition to printing text, you may use cout statements to output variables. Variables are covered in more detail in the next chapter. In the following program, two integer variables (A and B) are declared and assigned values of 2 and 3, respectively.
#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; } |
I will now print the values of A and B. 2 3 |
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. Note that the endl can be added to the same line with and extra <<.
#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; } |
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 next program listing uses variables in the 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; } |
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, the equation follows the order of operations. 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.
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 by adding another <<. A good practice is to have one cout per line of output.
#include <iostream> using namespace std; main() { cout << "This program has one line per cout\n"; cout << "8 + 3 = " << 8 + 3 << endl; cout << "20 % 7 = " << 20 % 7 << endl; } |
This program has one
line per cout 8 + 3 = 11 20 % 7 = 6 |
// 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; } |
The product of 18 and 3 is 54 |
#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; */ } |
Welcome! |
Using two backslashes \\ is the C++ line style. The older C block comments style 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.
The number of digits past the decimal can be set using setprecision in the iomanip library. The program below rounds the output to two decimal places.
#include <iostream> #include <iomanip> using namespace std; int main () { float A=12.526, B=.009, C=1.2; cout << fixed << setprecision(2); cout << A << " " << B << " " << C << endl; return 0; } |
12.53 0.01 1.20 |