Using cout in C++

By: Baski Viewed: 153529 times  Printer Friendly Format    


To print a value to the screen, write the word cout, followed by the insertion operator (<<), which you create by typing the less-than character (<) twice. Even though this is two characters, C++ treats it as one.

Follow the insertion character with your data. Listing below illustrates how this is used. Type in the example exactly as written, except substitute your own name where you see Baski (unless your name is Baski, in which case leave it just the way it is; it's perfect).

Using cout.

1:     // Listing 2.2 using cout
2:
3:     #include <iostream.h>
4:     int main()
5:     {
6:        cout << "Hello there.\n";
7:        cout << "Here is 5: " << 5 << "\n";
8:        cout << "The manipulator endl writes a new line to the screen." <<  
                      Âendl;
9:        cout << "Here is a very big number:\t" << 70000 << endl;
10:       cout << "Here is the sum of 8 and 5:\t" << 8+5 << endl;
11:       cout << "Here's a fraction:\t\t" << (float) 5/8 << endl;
12:       cout << "And a very very big number:\t" << (double) 7000 * 7000 << 
                      Âendl;
13:       cout << "Don't forget to replace Baski with your name...\n";
14:       cout << "Baski is a C++ programmer!\n";
15:        return 0;
16: }

Hello there.
Here is 5: 5
The manipulator endl writes a new line to the screen.
Here is a very big number:      70000
Here is the sum of 8 and 5:     13
Here's a fraction:              0.625
And a very very big number:     4.9e+07
Don't forget to replace Baski with your name...
Baski is a C++ programmer!

On line 3, the statement #include <iostream.h> causes the iostream.h file to be added to your source code. This is required if you use cout and its related functions.

On line 6 is the simplest use of cout, printing a string or series of characters. The symbol \n is a special formatting character. It tells cout to print a newline character to the screen.

Three values are passed to cout on line 7, and each value is separated by the insertion operator. The first value is the string "Here is 5: ". Note the space after the colon. The space is part of the string. Next, the value 5 is passed to the insertion operator and the newline character (always in double quotes or single quotes). This causes the line

Here is 5: 5

to be printed to the screen. Because there is no newline character after the first string, the next value is printed immediately afterwards. This is called concatenating the two values.

On line 8, an informative message is printed, and then the manipulator endl is used. The purpose of endl is to write a new line to the screen. (Other uses for endl are discussed on Day 16.)

On line 9, a new formatting character, \t, is introduced. This inserts a tab character and is used on lines 8-12 to line up the output. Line 9 shows that not only integers, but long integers as well can be printed. Line 10 demonstrates that cout will do simple addition. The value of 8+5 is passed to cout, but 13 is printed.

On line 11, the value 5/8 is inserted into cout. The term (float) tells cout that you want this value evaluated as a decimal equivalent, and so a fraction is printed. On line 12 the value 7000 * 7000 is given to cout, and the term (double) is used to tell cout that you want this to be printed using scientific notation. All of this will be explained on Day 3, "Variables and Constants," when data types are discussed.

On line 14, you substituted your name, and the output confirmed that you are indeed a C++ programmer. It must be true, because the computer said so!



Most Viewed Articles (in C++ )

Latest Articles (in C++)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Great tutorial. Thanks guys.
View Tutorial          By: John D at 2009-03-21 11:01:58

2. i want to say only one thing

great

View Tutorial          By: ashish at 2009-06-09 21:36:45

3. It would be nice to add the line,


View Tutorial          By: Anonymous at 2009-08-30 07:59:10

4. Good example. However as someone said, it would be
View Tutorial          By: Neo Cambell at 2010-03-10 11:02:06

5. sir needed help i am compiler dev c++ which shown
View Tutorial          By: shahab at 2010-05-05 13:17:53

6. fantastic tutorial and very well explained, I've o
View Tutorial          By: Francis at 2011-08-14 19:57:18

7. cout<<5+"object orinted programming
View Tutorial          By: SANDEEP SHARMA at 2011-09-18 19:06:57

8. Great tutorial.I have one question.What is the dif
View Tutorial          By: Curious at 2011-10-21 16:16:21

9. Line 11 says:
cout << "Here's a

View Tutorial          By: Nrom at 2012-06-26 20:16:52