Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Input and output in C++

3,845 views

Published on

Handling I/O in C++

Published in: Education
  • Login to see the comments

Input and output in C++

  1. 1. OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg wwiitthh CC++++ IInnppuutt aanndd OOuuttppuutt iinn CC++++ By Nilesh Dalvi LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. http://www.slideshare.net/nileshdalvi01
  2. 2. Introduction • Every program takes some data as input and generates processed data as output. • C++ supports set of I/O functions. • C++ uses the concepts of stream and stream classes to implement its I/O operations with console and disk files. • In this chapter we will discuss how stream classes support the console-oriented I/O operations. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  3. 3. Streams in C++ • Stream is a sequence of bytes. • If data is received from input devices in sequence then it is called as source stream. • When data is passed to output devices then it is called destination. • The data is received from keyboard or disk and can be passed on to monitor or to the disk. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  4. 4. Streams in C++ Streams in I/O devices Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  5. 5. Streams in C++ • Data in source stream can be used as input data by program . • Source stream is called as input stream. • Destination stream that collects output data from the program is known as output stream. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  6. 6. Streams in C++ Input and output streams Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  7. 7. Streams Classes • C++ streams based are based on class and object theory. • C++ has a number of stream classes that are used to work with console and file operations. • These classes are known as streams classes. • All these classes are declared in the header file <iostream>. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  8. 8. Streams Classes Hierarchy of streams classes Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  9. 9. Streams Classes • Classes istream and ostream are derived classes of base class ios. • streambuf handles the buffer by providing the facilities to flush and pour the buffer. • iostream is derived from classes istream and ostream by using multiple inheritance. • ios class is a virtual class which avoid ambiguity • ios class has an ability to handle formatted and unformatted I/O operations. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  10. 10. Streams Classes Class Name Contents ios • Contains basic facilities that are used by all other input and output classes istream • Inherits properties of ios. • Declares input function get(), getline(), read(). • Contains overloaded extraction >> operator ostream • Inherits properties of ios. • Declares input function put(), write(). • Contains overloaded insertion << operator iostream • Inherits properties of ios, istream and ostream. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  11. 11. Unformatted console I/O operations Input and output streams • Input stream uses cin object to read data • Output stream uses cout object to display data on the screen. • Data type is identified by these functions using operator overloading << (insertion) and >> (extraction). • The >> operator is overloaded in istream class • The << operator is overloaded in ostream class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  12. 12. Working of cin and cout statements Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  13. 13. Unformatted console I/O operations Input Stream • It does read operation through keyboard. • It uses cin object. • cin statement uses >> operator before variable name. • Syntax: cin >> variable; • Example: int weight; cin >> weight; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  14. 14. Unformatted console I/O operations Output Stream • It displays the contents of variables on the screen. • It uses cout object. • cout statement uses << operator before variable name. • Syntax: cout << variable; • Example: int weight; cout << weight; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  15. 15. Unformatted console I/O operations get() and put() • The single character input and output operations in C++ can be done with get() and put() functions. • get() – reads character. • put() – display the character. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  16. 16. Unformatted console I/O operations The get() has two syntaxes. • get(char) – assigns the input character to its argument. char c; cin.get(c); //get char from keyboard. while(c != 'n') { cout << c; //display char on screen. cin.get(c); //get another char. } Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  17. 17. Unformatted console I/O operations The get() has two syntaxes. • get(void) – returns the input character. char c; c = cin.get(); value returned by function get() is assigned to c. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  18. 18. Unformatted console I/O operations • The function put() can be used to output a line of text, character by character. cout.put(‘X’); • Displays the character X. cout.put(68); • This statement will convert int value 68 to char value and display the character whose ASCII value is 68. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  19. 19. Character I/O with get() and put() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  20. 20. Unformatted console I/O operations getline() and write() functions: • Displays a line of text by using line-oriented I/O functions getline() and write(). • getline()- reads a whole line of text that ends with a newline character(‘n’). • Invoked by using object cin as follows: cin.getline (line, size); • Reading is terminated as the newline char ‘n’ is encountered or size-1 characters are read. • Newline character is read but not saved. • Instead, it is replaced by null character. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  21. 21. Reading Strings with getline() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  22. 22. Unformatted console I/O operations • write() function displays an entire line and has the following form: cout.write (line, size); • line – represents the name of the string to be displayed . • size - indicates number of characters to display. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  23. 23. Displaying String with write() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  24. 24. Formatted Console I/O Operations • C++ provides various formatted console I/O functions for formatting the output. 1. ios class functions and flags. 2. Manipulators 3. User-defined output functions • ios grants operations common to both input and output. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  25. 25. Formatted Console I/O Operations Function Working width() To set required field width. o/p will be displayed with given width. precision() To set number of decimal point for a float value. fill() To set character to fill in the blank space of the field. setf() To set various flags for formatting output. unsetf() To remove the flags setting Table: ios class functions Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  26. 26. Formatted Console I/O Operations Fig: Formatted functions with cout object Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  27. 27. Defining field width: width() • width() – defines width of a field necessary for the output of an item. cout.width (w); • w - field width(number of columns). • Output will be printed in a field of w characters wide at the right end of the field. • Field width should be specified for each item separately. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  28. 28. Defining field width: width() • For example, the statements cout.width(5); cout<<543<<12<<"n"; will produce following output: 5 4 3 1 2 • 543 is printed right-justified in the first five columns. • width(5) does not retain the setting for printing the 12. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  29. 29. Defining field width: width() • For example, the statements cout.width(5); cout<<543; cout.width(5); cout<<12<<"n"; This produce the following output: 5 4 3 1 2 • C++ never truncates the values and • If the specified width is smaller than the size of the value, C++ expands the field to fit the value. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  30. 30. Specifying field size with width() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  31. 31. Setting precision : precision() • Default floating numbers are printed with six digits after the decimal point. • precision() – specifies number of digits to be displayed after the decimal point while printing the floating-point numbers, has the form: cout.precision(d); • d is the number of digits to the right of the decimal point. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  32. 32. Setting precision : precision() • For Examples: cout.precision(3); cout<<1.23456<<”n”; cout.precision(4); cout<<3.14159<<”n”; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  33. 33. width() and precision() • We can also combine the field specification with the precision setting. Example: cout.precision(2); cout.width(5); cout<<1.2345; • The output will be: 1 . 2 3 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  34. 34. Setting precision : precision() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  35. 35. Filling and Padding :fill() • We can use the fill() function to fill the unused positions by any desired character. • It is used in the following form: cout.fill(ch); • where ch represents the character which is used for filling the unused positions. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  36. 36. Filling and Padding :fill() • For Example: cout.fill(‘*’); cout.width(10); cout<<5250<<"n“; • The output would be: * * * * * * 5 2 5 0 • Financial institutions and banks use this kind of padding while printing cheques so that no one can change the amount easily. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  37. 37. Formatting flags, bit-fields and setf() • setf() function can be used as follows: cout.setf(arg1,arg2); • arg1- one of the formatting flags defined in the class ios. • It specifies the format action required for the output. • arg2- Another ios constant known as bit field specifies the group to which the formatting flag belongs. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  38. 38. Table : Format-state-flags Flag Bit Field Flag Purpose ios::left ios::right Right-Justify all Output ios::adjustfield ios::internal Left-Justify sign or Base indicator and Left-Justify all Output right-justify rest of the number Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). ios::dec ios::basefield Displays integer in base 10(decimal) format ios::oct Displays integer in base 8(octal) format ios::hex Displays integer in base 16(hexadecimal) format ios::fixed ios::floatfield Use fixed point notation when displaying floating point numbers ios::scientific Use exponential notation when displaying floating point numbers
  39. 39. Formatting flags, bit-fields and setf() • Consider the following segment of code: cout.fill(‘*’); cout.setf(ios::left,ios::adjustfield); cout.width(10); cout<<"TABLE 1"<<"n"; • This will produce the following output: T A B L E 1 * * * Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  40. 40. Formatting flags, bit-fields and setf() • The statements cout.fill('*'); cout.precision(3); cout.setf(ios::internal,ios::adjustfield); cout.setf(ios::scientific,ios::floatfield); cout.width(15); cout<<-12.34567<<“n”; • will produce the following output: - * * * * * 1 . 2 3 5 e + 0 1 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  41. 41. Displaying Trailing Zeros And Plus Sign • If we print the numbers 10.75, 25.00 and 15.50 using, cout.width(8); cout.precision(2); • Then output will be as follows: 1 0 . 7 5 2 5 1 5 . 5 • The trailing zeros in the second and third items have been truncated. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  42. 42. Displaying Trailing Zeros And Plus Sign • The above output would look better if they are printed as follows: 10.75 25.00 15.50 • setf() can be used with the flag ios::showpoint as a single argument to achieve this form of output. • For example, cout.setf(ios::showpoint); • which display tailing zeros Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  43. 43. Displaying Trailing Zeros And Plus Sign • plus sign can be printed before a positive number using the following statement: cout.setf(ios::showpos); • showpoint and showpos do not have any bit fields and therefore are used as single arguments in setf(). Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  44. 44. Displaying Trailing Zeros And Plus Sign • For example, the statements cout.setf(ios::showpoint); cout.setf(ios::showpos); cout.precision(3); cout.setf(ios::fixed,ios::floatfield); cout.setf(ios::internal,ios::adjustfield); cout.width(10); cout<<275.5<<"n"; • will produce the following output: + 2 7 5 . 5 0 0 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  45. 45. Flag Lists • Following table lists the flags that do not posses a named bit field: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  46. 46. Formatting with flags in setf() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  47. 47. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  48. 48. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  49. 49. Managing output with manipulators: • The header file iomanip provides a set of functions called manipulators which can be used to manipulate the output formats. • Two or more manipulators can be used as a chain in one statement as shown below: cout<<manip1<<manip2<<manip3<<item; cout<<manip1<<item1<<manip2<<item2; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  50. 50. Managing output with manipulators: • The most commonly used manipulators are shown in table. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  51. 51. Managing output with manipulators: • One statement can be used to format output for two or more values. • For example, the statement cout<<setw(5)<<setprecision(2)<<1.2345 <<setw(10)<<setprecision(4)<<sqrt(2) <<setw(15)<<setiosflags(ios::scientific) <<sqrt(3)<<endl; • Will print all the three values in one line with the field size of 5,10,and 15 respectively. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  52. 52. Managing output with manipulators: • We can jointly use the manipulators and the ios functions in a program: cout.width(5); cout<<setprecision(2)<<1.2345 <<setw(10)<<setprecision(4)<<sqrt(2) <<setw(15)<<setiosflags(ios::scientific) <<sqrt(3)<<endl; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  53. 53. Designing Our Own Manipulators • General form for creating a manipulator is: ostream & manipulator(ostream & output) { ..... .....(code) ..... return output; } • Here, the manipulator is the name of the manipulator Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  54. 54. Designing Our Own Manipulators • The following function defines a manipulator called unit that displays "inches": ostream & unit(ostream & output) { output<<"inches"; return output; } • The statement cout << 36 << unit; • will produce the following output 36 inches Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  55. 55. Designing Our Own Manipulators • We can also create manipulators that could represent a sequence of operations. Example: ostream & show(ostream & output) { output.setf(ios::showpoint); output.setf(ios::showpos); output<<setw(10); return output; } • Manipulator show turns on the flags showpoint and showpos and sets the field width to 10. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  56. 56. Designing Our Own Manipulators #include <iostream> #include <iomanip> ostream & curr(ostream & ostr) { cout<< setprecision(2); cout<<”Rs. “; return ostr; } int main() { float amt = 4.5476; cout<<curr<<amt; return 0; } //Output: Rs. 4.55 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).

×