Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming."— Presentation transcript:

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.

2 2 Simple Input/Output When 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.

3 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.

4 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

5 Simple Input/Output  Example of printing on screen with << operator 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include 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; }

6 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).

7 7 Simple Input/Output  Both > 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 6 7 8 9 10 11 12 13 14 #include 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; }


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

Similar presentations


Ads by Google