Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml.

Similar presentations


Presentation on theme: "Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml."— Presentation transcript:

1 Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml

2 Objectives The contents we are going to cover in this chapter:  Input and Output  Standard Output  Escape Sequences  C++ Manipulators  Standard Input

3 Input & Output Input Operation The process of giving something to computer usually with the help of keyboard is called input. A program may need certain type of input data to perform its functionality. The term standard input refers to input via keyboard. Output Operation The process of getting something from computer usually on monitor screen is called output. The term standard output is refers to output displayed on the monitor. As, the result of a program is the output of the program displayed on the monitor screen, In C++, different streams are available by the developers of the language to perform certain type of input output operation. Stream – Flow Of Data These are the objects used by the program to insert or extract characters. C++ library includes the header file iostream.h Iostream.h contains the declarations of all the standards input and output objects. So header file iostream.h must be included in a program to perform standard input and output. CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input

4 Standard Output As the term Standard Output refers to the output displayed on the monitor. C++ uses cout object (stands for “console output” & read as “see-out”) to display standard output. The cout object is used with insertion operator ( << ). Any string, value or constant followed by the << is displayed on the screen. Syntax: cout << variable / constant / expression ; Example includes as follows ocout << var1;// variable ocout << 10;// constant ocout <<“C++ Strong Programming Language!”; // string value CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Message << cout

5 Standard Output Examples CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Output: testing output…10* Output: 24.27 + 41.50 = 65.77

6 Standard Output CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Escape Sequences Escape sequence are special character used in Control String. These are used to modify the format of output. These are not displayed on the screen in output. These special characters are comes with the combinations of back slash ‘/’ known as escape character Following table shows the Escape Sequences in C++

7 Standard Output Examples CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Using one cout-statement print Asterisks in this format. * * * * * * * * * * * Solution: (we use escape sequence new line i.e. “\n” ) #include int main() { cout<<“*\n**\n***\n****\n*****”; return 0; }

8 C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input C++ Manipulators C++ manipulators are used to format the output in different styles. These the common controls to handle the output issues. These manipulators enable the user to make output in desired format which one is appropriate. Following table contains some important C++ manipulators: Now we discuss each of the manipulators one by one in details

9 C++ Manipulators endl() Manipulator (iostream.h) The word ‘endl’ stands for end-of-line. The endl manipulator is used to move the cursor to beginning of the next line in the output. It works similar to ‘\n’ escape sequence. It requires no parameters. Syntax: cout << variable / expression / constant << endl; Example is as follows #include Int main() { clrscr(); cout << “Hello”<<endl<<“World!”; cout << endl << endl << “Hello World!”; return 0; } CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Hello World! Hello World!

10 C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input setw() Manipulator (iomanip.h) The word setw stands for set width. The setw manipulator is used to display the value of an expression in specified columns. The value to displayed can be a string or any numeric value i.e. integer, floating-point number, CONSTANT etc… If the value of the expression is less than the additional columns are left blank from left side. The output automatically uses the required columns if the output is larger than the specified columns. (In this case the setw manipulator has no effect on the output) Syntax: setw( int n); The integer n indicates the number of columns in which the value is to be displayed. It can be unsigned positive integer constant, variable or expression. Suppose variable a = 33 and variable b = 7132 a)cout << setw(4) << a << setw(5) << b << setw(4) << “Hi”; b)cout << setw(2) << a << setw(4) << b << setw(2) << “Hi”; c)cout << setw(6) << a << setw(5) << b << setw(3) << “Hi”; d)cout << setw(1) << a << setw(5) << b << setw(1) << “Hi”; Note: This table is just for illustration purpose otherwise in real scenario screen of a normal monitor is 80 cells in width and 25 cells in height i.e. 80x25 characters

11 C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Setw( int n ) Example #include int main() { int a = 33; int b = 7132; cout << a << b << "Hi" <<endl; cout << a <<" "<< b <<" "<< "Hi" <<endl; cout << "\nNow we use C++ Manipulators in different ways\n" <<endl; cout << setw(4) << a << setw(5) << b << setw(4) <<"Hi"<<endl; cout << setw(3) << a << setw(5) << b << setw(3) <<"Hi"<<endl; cout << setw(6) << a << setw(5) << b << setw(3) <<"Hi"<<endl; cout << setw(1) << a << setw(5) << b << setw(1) <<"Hi"<<endl; return 0; } 337132Hi Now we use C++ Manipulators in different ways 33 7132 Hi Press any key to continue...

12 C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input setprecision() Manipulator The setprecision manipulator is used to set the number digits to be displayed after decimal point. It is applied to all subsequent floating-point numbers written to that output stream. The value is rounded with the use of this manipulator. Syntax: cout << setprecision(int) << variable ; // accepts positive integers Example 4.918766258 4.91876626 4.9187663 4.918766 4.91877 4.9188 4.919 4.92 4.9 5 Press any key to continue... #include int main() { double result, n1 = 132.364, n2 = 26.91; result = 0.0; result = n1 / n2 ; cout << setprecision(10)<<result<<endl; cout << setprecision(9)<<result<<endl; cout << setprecision(8)<<result<<endl; cout << setprecision(7)<<result<<endl; cout << setprecision(6)<<result<<endl; cout << setprecision(5)<<result<<endl; cout << setprecision(4)<<result<<endl; cout << setprecision(3)<<result<<endl; cout << setprecision(2)<<result<<endl; cout << setprecision(1)<<result<<endl; return 0; }

13 C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input fixed() Manipulator The fixed manipulator is used to further control the output of the floating point numbers. It displays the floating point number in a fixed decimal format. To use this manipulator we execute statement as follows cout << setiosflags(ios::fixed) << variable; All floating point number displayed afterwards – will maintain their fixed decimal format i.e. their will not any other format like scientific or exponential format. To disable this type of flags we execute similar statement as follows cout << resetiosflags(ios::fixed) << variable; Example: In C++ on the basis of default settings - The value 10000000 is displayed as 1e+07 in exponential format which is quite harder to understand in first look. Here is the situation where we have to manage output in understandable format by using appropriate manipulators provided by C++ Developers. Solution: simple we use fixed manipulator as cout << setiosflags(ios::fixed) << 10000000; // output as mentioned

14 C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input fixed manipulator example #include int main() { long double var1 = 10000000; cout << "Without using fixed manipulator" <<endl; cout << "var1 = " <<var1 << endl; cout << "\nNow we use fixed manipulator" <<endl; cout << "var1 = "<< setiosflags(ios::fixed)<<var1<<endl; cout << "\nNow we use fixed and setprecision(2) manipulators" <<endl; cout << "var1 = “ <<setiosflags(ios::fixed)<<setprecision(2)<<var1<<endl; cout << "\nWe Change the value of var1 = 17855143"<<endl; var1 = 17855143; cout <<"Now we use fixed and setprecision(2) manipulators" <<endl; cout <<"var1 = “ <<setiosflags(ios::fixed)<<setprecision(2)<<var1<<endl<<endl; return 0; }

15 C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input showpoint() Manipulator By default decimal part of a floating point number is not displayed if it is zero. In a situation where it is necessary to show decimal part also to achieve accuracy i.e. in temperature records or to show CGPA (Cumulative Grade Point Average) of students Syntax: cout << setiosflags(ios::showpoint)<<variable or value; #include int main() { float points = 3.0; cout<<"Without using Manipulators"<<endl; cout<<"CGPA = "<<points<<endl; cout<<"\nUsing setiosflags(ios::showpoint)"<<endl; cout<<"CGPA = "<<setiosflags(ios::showpoint)<<points<<endl; cout<<"\nUsing two manipulators: setprecision(3) and setiosflags(ios::showpoint)"<<endl; cout<<"CGPA = "<<setprecision(3)<<setiosflags(ios::showpoint)<<points<<endl; cout<<"\nCGPA has been changed from 3 to 3.5"<<endl; points = 3.5; cout<<"\nUsing two manipulators: setprecision(3) and setiosflags(ios::showpoint)"<<endl; cout<<"CGPA = "<<setprecision(3)<<setiosflags(ios::showpoint)<<points<<endl<<endl; return 0; }

16 C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input setfill() Manipulator The setfill() manipulator is used to replace the leading or trailing blanks in the output by the specified characters. It requires one parameter to specify the fill character. The parameter can be a letter, digit or constant as a character i.e. in single quotes like ‘0’ or ‘@’. #include int main() { int var1 = 20; cout << var1 << endl; cout << setw(5) << var1 << endl; cout << setfill('#') << setw(05) << var1 <<endl; cout << setfill(' ') << setw(20) << var1 <<endl; cout << setfill('0') << setw(20) << var1 <<endl; return 0; }

17 Standard Input CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input The term standard input refers to the input given via keyboard. C++ uses cin (stands for C onsole In put and read as “see-in”) stream object to perform standard input operation. C++ handles standard input operation by applying extraction operator “>>” with cin object. The “>>” is called extraction operator because it extracts the value from input stream and store it in the memory. The “>>” operator must be followed by a variable. The variable very next to extraction operator “<<” is used to store the value. Syntax: cin >> var1 >> var2 >> varN; If there are more than one variable on the left of >> as shown here then the first value will store in var1 and so on… #include int main() { char name[20], cityName[20]; int age = 0; cout <<"Please Enter Your Name: "; cin >> name; cout <<"Please Enter Your Age: "; cin >> age; cout <<"Please Enter name of your City: "; cin >> cityName; cout <<"\n\nHello \""<<name<<"\"! How are you?"; cout <<"\nYour age is \""<<age<<"\" thats good."; cout <<"\nYour City is \""<<cityName<<"\" - Have a nice day!"<<endl; return 0; } Please Enter Your Name: Ali Please Enter Your Age: 22 Please Enter name of your City: GUJRANWALA Hello "Ali"! How are you? Your age is "22" thats good. Your City is "GUJRANWALA" - Have a nice day! Press any key to continue...

18 Standard Input CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Standard Input Example Input two Integers, print them on screen then swap them and again print on screen. #include int main() { int num1, num2; int temp; // used while swapping cout << "Enter First Number: "; cin >> num1; cout << "Enter Second Number: "; cin >> num2; cout << "\nFirst Number : "<<num1<<endl; cout << "Second Number : "<<num2<<endl; temp = num1; num1 = num2; num2 = temp; cout << "\nAfter Swapping\n\nFirst Number : "<<num1<<endl; cout << "Second Number : "<<num2<<endl; cout<<endl<<endl; return 0; } Enter First Number: 10 Enter Second Number: 18 First Number : 10 Second Number : 18 After Swapping First Number : 18 Second Number : 10 Press any key to continue...

19 Standard Input CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Standard Input Example #include 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; } Please enter an integer value: 702 The value you entered is 702 and its double is 1404.


Download ppt "Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml."

Similar presentations


Ads by Google