Evo. G Tech Team Forum
Welcome to Evo. G Tech Team Forum. We have moved to a new website : www.evogtechteam.com

Thanks you.

by Evo. G Tech Team Management.

Basic Input/Output

View previous topic View next topic Go down

Basic Input/Output

Post by Rachael_C on August 30th 2014, 02:39

The example programs of the previous tutorial provided little interaction with the user which just simply printed simple values on screen, but the standard library provides many additional ways to interact with the user via its input/output features.

C++ uses a convenient abstraction called streams to perform input and output operations.
Streams are a source of characters, and that these characters are provided/accepted sequentially.

cin => standard input stream
cout => standard output stream


Standard output (cout)

On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout.

For formatted output operations, cout is used together with the insertion operator, which is written as <<.

[You must be registered and logged in to see this image.]

Multiple insertion operations (<<) may be chained in a single statement:

[You must be registered and logged in to see this image.]

Assuming the age variable contains the value 18 and the ID variable contains 1234, the output of the previous statement would be:

[You must be registered and logged in to see this image.]

What cout does not do automatically is add line breaks at the end, unless instructed to do so. For example, take the following two statements inserting into cout:

[You must be registered and logged in to see this image.]

The output would be in a single line, without any line breaks in between. Something like:

[You must be registered and logged in to see this image.]
To insert a line break, a new-line character shall be inserted at the exact position the line should be broken. In C++, a new-line character can be specified as \n. For example:

[You must be registered and logged in to see this image.]

This produces the following output:

[You must be registered and logged in to see this image.]

Alternatively, the endl manipulator can also be used to break lines. It produces a newline character, exactly as the insertion of '\n' does. For example:

[You must be registered and logged in to see this image.]

This would print:

[You must be registered and logged in to see this image.]


Standard input (cin)

In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin.

For formatted input operations, cin is used together with the extraction operator, which is written as >>. For example:

[You must be registered and logged in to see this image.]

The first statement declares a variable of type int called age, and the second extracts from cin a value to be stored in it. The program will wait for the user to enter some sequence with the keyboard.

Example input/output program:

[You must be registered and logged in to see this image.]

You should get:

[You must be registered and logged in to see this image.]

Rachael_C
Beginner
Beginner

Posts : 13
Points : 23781
Reputation : 0
Join date : 2014-04-27

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum