1 Simple Input/Output C++ offers the iostream library, which defines a system of character-oriented...

7
1 Simple Input/Output C++ offers the iostream library , which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

Transcript of 1 Simple Input/Output C++ offers the iostream library, which defines a system of character-oriented...

Page 1: 1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

1

Simple Input/Output

C++ offers the iostream library , which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

Page 2: 1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

2

Simple Input/OutputWhen a C++ program that includes the iostream

classes starts, four objects are created and initialized: cin handles input from the standard input, the

keyboard. cout handles output to the standard output, the

screen. cerr handles unbuffered output to the standard error

device, the screen. clog handles buffered error messages that are output

to the standard error device, the screen.

Page 3: 1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

3

Simple Input/Output Printing output

To send data to standard output, use the operator <<.

For example:

cout << "cout example !";

sends the string “cout example” to the object called cout.

Page 4: 1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

4

Simple Input/Output

The operator << is overloaded with a variety of meanings when used with cout, so you can print on the screen different arguments: Characters Strings Integers Floating-point numbers Addresses

Page 5: 1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

Simple Input/Output Example of printing on screen with << operator

1 2 3 4 5 678910111213141516

17

#include <iostream.h>int main (void){cout << "It will be printed different type of data:\n";cout << "string: " << "abcd" <<endl;cout << "char: " << 'a' << endl;cout << "char: " << char(97) <<endl;cout << "non-printing char (escape): "<< char(27) << endl;cout << "integer: " << 123 <<endl;cout << "long: " << -1234567 <<endl;cout << "double: " << 123.456 << endl;cout << “\n\n " ;cout << "Here is a very big number:\t" << 70000 << endl; cout << "Here is the sum of 8 and 5:\t" << 8+5 << endl; cout << "Here's a fraction:\t\t" << (float) 5/8 << endl; cout << "And a very very big number:\t" << (double) 7000 * 7000 << endl; }

Page 6: 1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

6

Simple Input/Output Reading input

To read data from standard input, use the operator >>.

For example:

int a;

cin >> a;

This operator waits for the same kind of input as its argument (in our example int).

Page 7: 1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.

7

Simple Input/Output Both << and >> enable multiple input or multiple

output operations and permit using of manipulators to change the format of I/O data .

1 2 3 4 5 67891011121314

#include <iostream.h>int main (void){ // Specifying formats with manipulators: int integer; cout << "input a number in decimal: " ; cin >> integer; cout << "in octal is: " << oct << integer << endl; cout << "in hex is: " << hex << integer << endl; cout << "input a number in octal: " ; cin >> oct >> integer; cout << "in decimal is: " << dec << integer << endl; cout << "in hex is: " << hex << integer << endl;}