Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture #5: iostream functions دوال الإدخال والإخراج Dr. Hmood Al-Dossari King Saud University Department of Computer Science 4 March 2012.

Similar presentations


Presentation on theme: "Lecture #5: iostream functions دوال الإدخال والإخراج Dr. Hmood Al-Dossari King Saud University Department of Computer Science 4 March 2012."— Presentation transcript:

1 Lecture #5: iostream functions دوال الإدخال والإخراج Dr. Hmood Al-Dossari King Saud University Department of Computer Science 4 March 2012

2 Overview  Input Functions (دوال الإدخال) cin.get(); cin.ignore(); cin.putback(); cin.peek(); cin.clear();  Output Functions (دوال الإخراج) setprecision(); fixed showpoint setfill setw(); left/right

3 ‘>>’ will skip all leading whitespace characters. Consider the variable declarations: char ch1, ch2; int num; Suppose, the input is: A 25 Consider the following statement: cin>>ch1>>ch2>>num; Output: I.‘A’ is stored in ch1, II.‘2’ is stored in ch2 and III.5 is stored in num. Note: the blank is skipped by the extraction operator ‘>>’,. Input Functions: cin.get()

4 Using get function with cin will take spaces into account. The syntax of cin statement with get function: cin.get(varChar); In previous example: cin.get(ch1); cin.get(ch2); cin>>num; Output: I.‘A’ is stored in ch1, II.a blank is stored in ch2 and III.25 is stored in num. Input Functions: cin.get()

5 Ignore function is used to discard a portion of the input. The syntax of cin statement with ignore function: cin.ignore(intExp, chExp); Example: char first, last;; first=cin.get(); cin.ignore(256,' '); last=cin.get(); cout<< "Your initials are :" << first << last<<"\n"; Suppose, the input is: Hmood Al-Dossari Output: Your initials are :HA Input Functions: cin.ignore()

6 putback function puts the character ch back to the input stream. Thus, the next extracted character will be ch. The syntax of cin statement with putback function: istream.putback(varChar); istream.get(varChar); Where, varChar is an input stream variable. Input Functions: istream.putback()

7 peek function reads and returns the next character without extracting it. The syntax of cin statement with peek function: variable = istream.peek(); Input Functions: istream.peek()

8 Input Functions: putback and peek

9 When an input stream enters the fail state, the system ignores all further I/O stream. Thus, programmer can use the stream function clear to restore the input to a working state. The syntax of cin statement with peek function: cin.clear(); Example: int a = 23; int b = 34; cin>>a>>b; cin.clear(); cout<<a<<“, "<<b; Input Functions: cin.clear() Suppose the input is: 12 R Output: 12, 34 Suppose the input is: 19 56 Output: 19, 56

10 أكتب مخرجات البرنامج التالي int a = 23; int b = 34; cin>>a>>b; cin.clear(); cout<<a<<“, "<<b; Activity 1 نشاط - 1- Suppose the input is: R 12 Output:

11 setprecision function is used to control the output of floating-point numbers. a maximum of six decimal places for default output of floating- point. The syntax of setprecision: cout<<setprecision(n); This function requires #include header file. Example: float a = 2.3889; cout<<Setprecision(3); cout<<a; Output Functions: setprecision() Output: 2.38

12 fixed sets the output of floating-point numbers in a fixed decimal format on the standard device. The syntax of fixed: cout<<fixed; Note: to disable the manipulator fixed function to default setting, you can use: cout.unsetf(ios::fixed); Example: float a = 2.3889; cout<<fixed; cout<<a; Output Functions: fixed Output: 2.38890

13 If the decimal part of a decimal number is zero, the output may not show the decimal point and the decimal part. showpoint function forces the output to show the decimal point and decimal part. The syntax of showpoint: cout<<showpoint; Example: float a = 2; cout<<fixed; cout<<showpoint; cout<<a; Output Functions: showpoint Output: 2.00000

14 setw is used to output the value of an expression in specific columns. The value of the expression can be either a string or a number. The syntax of setw: cout<<setw(n)<<“............”; Example: cout<<setw(12)<<“Hmood”; Output Functions: setw() 123456789101112 domHo

15 setfill is used to allow us to fill the unused columns with a character other than a space. The syntax of setfill: cout<<setfill(‘&’); Example: cout<<setfill’&’; Output Functions: setfill() 123456789101112 domHo &&&&&&&

16 left is used for left-justified. The syntax of left: cout<<left; We can disable left function by using the stream function unsetf. ostream.unsetf(ios::left); Example: cout<<left; cout<<setw(12)<<“Hmood”; Output Functions: left function 123456789101112 doomH

17 right is used for right-justified. The syntax of right: cout<<right; We can disable right function by using the stream function unsetf. ostream.unsetf(ios::right); Example: cout<<right; cout<<setw(12)<<“Hmood”; Output Functions: right function 123456789101112 domHo

18 أكتب مخرجات البرنامج التالي cout<< cout<<right; cout<<setw(7)<<“Ali \n”; Cout<<left; Cout<<setw(11)<<“MMM”; --------------------------------------- float a=7.334; cout<<setprecision(3); cout<<fixed; cout<<“a=”<<a; Activity 2 نشاط - 2-


Download ppt "Lecture #5: iostream functions دوال الإدخال والإخراج Dr. Hmood Al-Dossari King Saud University Department of Computer Science 4 March 2012."

Similar presentations


Ads by Google