Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.

Similar presentations


Presentation on theme: "CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi."— Presentation transcript:

1 CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi

2 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. A preprocessor statement is a statement before the main program, which begins with # symbol. stdio.h is a header file that contains the built in program of these standard input output function.

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

4 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”.

5 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

6 Example #include using namespace std; void main( ) { int a,b,c; float d; cout<<”Enter three numbers”; cin>>a>>b>>c; d=(a+b+c)/3; cout<<”The average= ”<<d; } Output:

7 Input Using cin cin is 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

8 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

9 Output using cout cout is 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.

10 Examples This produces the same result as the previous sample cout << number_of_bars ; cout << " candy bars\n"; Here arithmetic is performed in the cout statement 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"

11 Designing Input and Output Prompt the user for input that is desired. cout statements 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;

12 Example #include using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }

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


Download ppt "CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi."

Similar presentations


Ads by Google