0% found this document useful (0 votes)
25 views18 pages

Chapter 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views18 pages

Chapter 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Engineering

1
2
3
4
FORMATTED INPUT / OUTPUT{
5
6
[Chapter 3]
7
8
9 ‘Objectives,
10 ▪ To understand and use various member functions for C++ formatted
11 I/O.
▪ To understand and use various stream manipulators for C++
12
13 } formatted I/O.

14

Programming Language
Engineering

1
2
3.1 iostream Library
▪ In this Module we will discuss how this formatted I/O implemented in C++ by using member functions and stream
3 manipulators.
▪ Discuss the formatted I/O here, for file I/O and some of the member functions mentioned in this Module, will be
4 presented in another Module.
5 ▪ The header files used for formatted I/O in C++ are:

6
7
8
9
10
11
12
13
14

Programming Language
Engineering

1 ▪ The compilers that fully comply with the C++ standard that use the template based header files won’t need
2 the .h extension. Please refer to Namespaces for more information.
▪ The iostream class hierarchy is shown below. From the base class ios, we have a derived class:
3
4
5
6
7
8 ▪ So, iostream support both stream input and output. The class hierarchy is shown below.
9
10
11
12
13
14 }
Programming Language
Engineering

1 3.2 Left and Right Shift Operators


2
3 ▪ We have used these operators in most of the previous tutorials for C++ codes.
▪ The left shift operator (<<) is overloaded to designate stream output and is called stream insertion operator.
4 ▪ The right shift operator (>>) is overloaded to designate stream input and is called stream extraction operator.
▪ These operators used with the standard stream object (and with other user defined stream objects) is listed below:
5
6
7
8
9
10
11
12
13
14

Programming Language
Engineering

1 ▪ For file processing C++ uses (will be discussed in another Module) the following classes:

2
3
4
5
6
7 ▪ Stream output program example:
// string output using <<
8 #include <iostream>
9 using namespace std;
int main()
10 {
cout<<"Welcome to C++ I/O module!!!"<<endl;
11 cout<<"Welcome to ";
cout<<"C++ module 18"<<endl;
12 // endl is end line stream manipulator
13 // issue a new line character and flushes the output buffer

}
// output buffer may be flushed by cout<<flush; command
14 return 0;
}

Programming Language
Engineering

1 ▪ Stream output program example:

2
// concatenating <<
3 #include <iostream>
using namespace std;
4 int main()
{
5 int p = 3, q = 10;
6
cout << "Concatenating using << operator.\n"
7 <<"--------------------------------"<<endl;
cout << "70 minus 20 is "<<(70 - 20)<<endl;
8 cout << "55 plus 4 is "<<(55 + 4)<<endl;
cout <<p<<" + "<<q<<" = "<<(p+q)<<endl;
9 return 0;
10 }

11
12
13
14 }
Programming Language
Engineering

1 ▪ Stream input program example:

2
#include <iostream>
3 using namespace std;

4 int main()
{
5 int p, q, r;
6 cout << "Enter 3 integers separated by space: \n";
cin>>p>>q>>r;}
7
// the >> operator skips whitespace characters such as tabs,
8 // blank space and newline.
cout<<"Sum of the "<<p<<","<<q<<" and "<<r<<" is = "<<(p+q+r)<<endl;
9 return 0;
10 }

11
12
13
14 }
Programming Language
Engineering

1 3.3 get() and getline() Member Functions of


2
3 Stream Input
4 ▪ For the get() function, we have three versions.
5
1. get() without any arguments, input one character from the designated streams including whitespace and returns
6 this character as the value of the function call. It will return EOF when end of file on the stream is encountered. For
example:
7 cin.get();
2. get() with a character argument, inputs the next character from the input stream including whitespace. It return
8 false when end of file is encountered while returns a reference to the istream object for which the get member
9 function is being invoked. For example:
char ch;
10 ...
cin.get(ch);
11
12
13
14

Programming Language
Engineering

1 3. get() with three arguments, a character array, a size limit and a delimiter (default value ‘\n’). It reads characters
from the input stream, up to one less than the specified maximum number of characters and terminates or terminates
2 as soon as the delimiter is read. For example:
3 char namevar[30];
...
4 cin.get(namevar, 30);
5 // get up to 29 characters and inserts null
6 // at the end of the string stored in variable
// namevar. If a delimiter is found,
7 // the read terminates. The delimiter
// is left in the stream, not stored in the array.
8
9 4. getline() operates like the third get() and insert a null character after the
line in the character array. It removes the delimiter from the stream, but does not
10 store it in the character array.
▪ A program examples:
11
12
13
14

Programming Language
Engineering

// get() version
1 #include <iostream>
2 using namespace std;

3 const int SIZE = 100;

4 int main()
{
5 char bufferOne[SIZE], bufferTwo[SIZE];
cout <<"Enter a line of text:"<<endl;
6 cin>>bufferOne;
// store the string in array bufferOne
7 // just the first word in the array string, then the
// first whitespace encountered
8 cout<<"\nThe line of text read by cin>> was:"<<endl;
t<<bufferOne<<endl;
9 cin.get(bufferTwo, SIZE);
// the rest of the string
10 cout<<"The line of text read by cin.get(bufferTwo,SIZE) was:"<<endl;
cout<<bufferTwo<<endl;
11 return 0;
}
12
13
14 }
Programming Language
Engineering

// getline() example
1 #include <iostream>
2 using namespace std;
const SIZE = 100;
3
int main()
4 {
char buffer[SIZE];
5
cout<<"Read by cin.getline(buffer, SIZE)\n"
6 <<"--------------------------------\n"<<"Enter a line of text:"<<endl;
cin.getline(buffer, SIZE);
7 cout<<"The line of text entered is: "<<endl;
cout<<buffer<<endl;
8 return 0;
}
9
10
11
12
13
14 }
Programming Language
Engineering

▪ ignore() member function skips over a designated number of characters (default is one character) or terminates
1 upon encountering a designated delimiter (default is EOF). For example:
2 cin.ignore(); // gets and discards 1 character.
cin.ignore(5); // gets and discards 5 characters.
3
4 cin.ignore(20,’\n’);
// gets and discards up to 20 characters or until
5 // newline character whichever comes first.

6 ▪ putback() member function places the previous character obtained by a get() on an


input stream back onto that stream. For example:
7 char chs;
8 ...
cin.putback(chs);
9 // put character back in the stream

10 ▪ peek() member function returns the next character from an input stream, but does
not remove the character from the stream. For example:
11 char chs;
12 ...
chs = cin.peek();
13 // peek at character

14

Programming Language
Engineering

1 ▪ Unformatted I/O performed with read() and write() member functions. They simply input or output as raw byte.
▪ The read() member function extracts a given number of characters into an array and the write() member function
2 inserts n characters (nulls included). For example:

3 char texts[100];
...
4 cin.read(texts, 100);
5 // read 100 characters from input stream and don’t append ‘\0’

6
7
8
9
10
11
12
13
14

Programming Language
Engineering

1 3.4 Stream Manipulators


2 ▪ Used to perform formatting, such as:
3 1. Setting field width.
2. Precision.
4 3. Unsetting format flags.
4. Flushing stream.
5 5. Inserting newline in the output stream and flushing the
stream.
6 6. Inserting the null character in the output stream.
7 7. Skipping whitespace.
8. Setting the fill character in field.
8
9 3.4.1 Stream Base
10 ▪ For stream base we have:

11
12
13
14 }
Programming Language
Engineering

▪ Program example:
1
// using hex, oct, dec and setbase stream manipulator
2 ##include <iostream>
#include <iomanip>
3 using namespace std;
4
int main()
5 {
int p;
6
cout<<"Enter a decimal number:"<<endl;
7 cin>>p;
8 cout<<p<< " in hexadecimal is: "
<<hex<<p<<'\n’
9 <<dec<<p<<" in octal is: "
<<oct<<p<<'\n’
10 <<setbase(10) <<p<<" in decimal is: "
11 <<p<<endl;
cout<<endl;
12 return 0;
}
13
14 }
Programming Language
Engineering

1 3.4.2 Floating-point Precision


2 ▪ Used to control the number of digits of an output stream display
▪ Use setprecision() or precision().
3
// using precision and setprecision
4 #include <iostream>
#include <iomanip>
5 // using C++ wrappers to access C function
#include <cmath>
6 using namespace std;
int main()
7 {
double theroot = sqrt(11.55);
8 cout<<"Square root of 11.55 with various"<<endl;
cout<<" precisions"<<endl;
9 cout<<"---------------------------------"<<endl;
cout<<"Using 'precision':"<<endl;
10 for(int poinplace=0; poinplace<=8; poinplace++)
{
11 cout.precision(poinplace);
cout<<theroot<<endl;
12 }
cout<<"\nUsing 'setprecision':"<<endl;
13 for(int poinplace=0; poinplace<=8; poinplace++)

}
cout<<setprecision(poinplace)<<theroot<<endl;
14 return 0;
}return 0;
}

Programming Language
{ THANK YOU
Any questions?

}
Engineering

1
2
3
4
5
6 John 3:16 – For God so loved the world, that He
7
8
gave His only begotten Son, that whoever
9 believes in Him, shall not perish but have
10 everlasting life.
11
12
13
14 }
Programming Language

You might also like