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.

Join the forum, it's quick and easy

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.
Evo. G Tech Team Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Basic Input/Output

Go down

Basic Input/Output Empty Basic Input/Output

Post by Rachael_C 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 : 73201
Reputation : 0
Join date : 2014-04-27

Back to top Go down

Back to top

- Similar topics

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