130 likes | 239 Views
Chapter 7 DATA INPUT OUTPUT. Prepared by: Lec . Ghader R. Kurdi. Introduction. For inputting and outputting data we use library function .the important of these functions are getch ( ), putchar ( ), scanf ( ), printf ( ), gets( ), puts( ).
E N D
Chapter 7DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi
Introduction • For inputting and outputting data we use library function .the important of these functions are getch( ), putchar( ), scanf( ), printf( ), gets( ), puts( ). • For using these functions in a C-program there should be a preprocessor statement #include<stdio.h>. • A preprocessor statement is a statement before the main program, which begins with # symbol. • stdio.his a header file that contains the built in program of these standard input output function.
Introduction • A data stream is a sequence of data. Typically in the form of characters or numbers. • An input stream is data for the program to use usually originates from, either the keyboard or a file. • An output stream is the program’s output destination would usually be, either the monitor a file.
Definitions • cout: The identifier cout is a predefined object that corresponds to standard output stream. • cin: The identifier cin is a predefined object that corresponds to standard input stream. • Escape sequence/ Escape character: Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits is called “escape sequence”.
I/O operators • The input operator(>>) is used to read value from standard input • The output operator(<<) is used to direct a value to standard output
Example #include<iostream> using namespace std; void main( ) { inta,b,c; float d; cout<<”Enter three numbers”; cin>>a>>b>>c; d=(a+b+c)/3; cout<<”The average= ”<<d; } Output:
Input Using cin • cinis an input stream bringing data from the keyboard. • The extraction operator (>>) removes data to be used. • Syntax: cin>>variable_name; • Example: cout<< "Enter the number of bars in a package\n"; cout<< " and the weight in ounces of one bar.\n"; cin>> number_of_bars; cin>> one_weight; • This code prompts the user to enter data then reads two data items from cin. The first value read is stored in number_of_bars. The second value read is stored in one_weight. Data is separated by spaces when entered
Reading Data From cin • Multiple data items are separated by spaces. Data is not read until the enter key is pressed. Allows user to make corrections. • Example: cin>> v1 >> v2 >> v3; Or cin>>v1; cin>>v2; cin>>v3; • Requires three space separated values • User might type 34 45 12 <enter key>
Output using cout • coutis an output stream sending data to the monitor. • The insertion operator "<<" inserts data into “cout”. • Example: cout<< number_of_bars << " candy bars\n"; • This line sends two items to the monitor. The value of number_of_bars. The quoted string of characters " candy bars\n". • Notice that the space before the ‘c’ in candy. The ‘\n’ causes a new line to be started following the ‘s’ in bars. A new insertion operator is used for each item of output.
Examples • This produces the same result as the previous sample cout<< number_of_bars; cout<< " candy bars\n"; • Here arithmetic is performed in the coutstatement cout<< "Total cost is $" << (price + tax); • Quoted strings are enclosed in double quotes ("Walter"). • Don’t use two single quotes ('). • A blank space can also be inserted with cout << " " ; if there are no strings in which a space is desired as in " candy bars\n"
Designing Input and Output • Prompt the user for input that is desired. • coutstatements provide instructions cout<< "Enter your age: "; cin>> age; • Echo the input by displaying what was read gives the user a chance to verify data cout<< age << " was entered." << endl;
Example #include <iostream> usingnamespacestd; intmain () { inti; cout<< "Please enter an integer value: "; cin>> i; cout<< "The value you entered is " << i; cout<< " and its double is " << i*2 << ".\n"; return0; }
Exercises Write a single C++ statement to accomplish each of the following: • Print the message “This is a C++ program” on one line. • Print the message “This is a C++ program” on two lines. End the first line with C++. • Print the message “This is a C++ program” with each word on a separate line. • Print the message “This is a C++ program” with each word separated from the next by a tab. • Declare the variable age to be of type int. • Prompt the user to enter his/her age. End your prompting message with : followed by space. • Read and integer from the user and store the value entered in the variable age.