Unit 2 - Basic Input, Output and Format Code

Posted on Friday, May 7, 2010 by B[H] | 0 comments
Labels:

In the iostream C++ library, standard input and output operations for a program are supported by two data streams:

a. cin (console input) for input – normally assigned to the keyboard
b. cout (console input) for output – normally directed to the screen.

By handling these two streams you will be able to interact with the user in your programs since you will be able to show messages in the screen and to receive his/her input from the keyboard.

1- Output (cout )

The function cout is used with the overloaded operator << (a pair of “less than” signs). It follows the following format as in the following examples: Photobucket

2.Input ( cin )

Handling the standard input in C++ is done by applying the overloaded operator of extraction ( >> ) on the cin stream, followed by the variable that will store the data that is going to be read. It follows the following format:
Photobucket


3-Format Code ( Backslash Character Constant)

The backslash character constant is also known as the ‘Escape Sequence’. Certain characters such as the backspace and carriage return are not printable characters and these characters pose special problems when printing using text editor. The backslash character constant is used together with other character constant when such problems are encountered.

Photobucket

Photobucket

This example shows the use of the new line (\n) character constant to print characters on the next line.

No comments:

Post a Comment