Lsp4you CPP Unit12
Lsp4you CPP Unit12
www.lsp4you.com
Operations
• C++ uses the concept of stream and
stream classes to implement its I/O
operations with the console and disk
files.
• C++ support all of C’s rich set of I/O
functions.
Learners Support Publications
www.lsp4you.com
C ++ Stream
C ++ Stream continue…
Program
Output Stream
Output device
Insertion into
output stream
C ++ Stream continue…
Program
Output Stream
Output device
Insertion into
output stream
• The data in the output stream can go to
the screen or any other storage device.
Learners Support Publications
www.lsp4you.com
C ++ Stream Classes
ios
iostream
Provides the
facilities for
iostream
formatted output
Provides the facilities
for handling both input
and output streams.
Manipulators
cout.width(w)
Where w is the field width (number of
columns).
The output will be printed in a field of w
characters wide at the right end of the field.
Learners Support Publications
www.lsp4you.com
cout.width(5);
cout << 543; The field should be specified for
each item separately.
cout.width(5);
cout << 12 << “\n”;
5 4 3 1 2
Learners Support Publications
#include <iostream>
www.lsp4you.com
using namespace std;
int main()
{
int items[4] = {10,8,12,15};
int cost[4] = {75,100,60,99};
cout.width(5);
cout « 'ITEMS';
cout.width(8);
cout « "COST"; PROGRAM 10.4
cout.width(15); The output of Program 10.4
cout « "TOTAL VALUE" « “\n”;
int sum = 0; would be:
for(int i-0; 1<4; i++) ITEMS COST TOTAL VALUE
{ 10 75 750
cout.width(5);
cout « items(i);
8 100 800
cout.width(8); 12 60 720
cout « cost(i); 15 99 1485
int value = items[i] * cost[i]; Grand Total = 3755
cout.width(15);
cout « value « "\n";
sum = sum + value;
}c
out « "\n Grand Total = ”;
cout.width(2);
cout «sum«"\n";
return 0;
}
Learners Support Publications
www.lsp4you.com
• cout.precision(d);
– Where d is the number of digits to the right of the
decimal point.
cout.precision(3);
1.141 truncated
cout << sqrt(2) << endl; 3.142 rounded
cout << 3.14159 << endl; 2.5 o trailing zeros
cout <<2.50032 << endl;
Learners Support Publications
www.lsp4you.com
cout.precision(2);
cout.width(5);
cout << 1.2345;
1 . 2 3
Learners Support Publications
#include <iostream> www.lsp4you.com
#include <cmath>
using namespace 5td;
int main()
{
cout « "Precision set to 3 digits \n\n”;
cout.precision(3);
cout.width(10);
cout « "VALUE";
cout.width(15);
cout « “SQRT_0F_VALUE” « "\n”;
for(int n=1; n<=5: n++)
{
cout.width(8);
cout « n;
cout.width(13);
cout « sqrt(n) « “\n”;
}c
out « "\n Precision set to 5 digits \n\n";
cout.precision(5); // precision parameter
changed
cout « " sqrt(10) = “ « sqrt(10) « “\n\n";
cout.precislon(0); // precision set to
default
cout « = sqrt(10) = “ « sqrt(10) « “
(default setting)\n";
return 0;
}
Learners Support Publications
www.lsp4you.com
cout.fill(ch);
where ch represents the character which is
used for filling the unused positions.
cout.fill(‘ * ’);
cout.wdith(10); Like precision( ), fill( ) stays
cout << 5250 << endl; in effect till we change it.
* * * * * * 5 2 5 0
Learners Support Publications
#include <iostream> www.lsp4you.com
using namespace Std;
int main( )
{
cout.fill('<');
cout.precision(3);
for(int n=1; n<=6; n++)
{
cout.width(5);
cout « n ;
cout.width(10);
cout « 1.0 / f1oat(n) « "\n\”;
if (n == 3)
cout.fill ('>');
}
cout « "\nPadding changed
\n\n";
cout.fill ('#'); // fill() reset
cout.width (15);
cout « 12.345678 « "\n";
return 0;
}
Learners Support Publications
www.lsp4you.com
Thank You