Transcript Chapter 1

Chapter 4
INPUT AND OUTPUT OBJECTS
Chapter 4
• The cout Object
The cout object is used to display
information to the monitor and is contained
in the iostream.h and iostream header files.
Chapter 4
• Using the cout Object
cout << item #1 << item #2 << item #3 << ··
<< item #n;
Chapter 4
• The cout Output Stream Flows to the
System Monitor
cout<< item #1 << item #2 << item #3 ... << item #n;
item #1
item #2
item #3
...
item #n
coutStream of Data Items to be Display ed
Monitor
Chapter 4
• Using \n and endl
cout << "HELLO\n";
OR
cout << "HELLO" << endl;
HELLO
Cursor returns to beginning of
next line.
cout << "HELLO";
HELLO Cursor remains at next position
on current line.
Chapter 4
• The C++ Escape Sequences Used with
cout
\a
\b
\n
\r
\t
\v
\\
\'
\”
\?
Beep
Backspace
CRLF
CR
Horizontal tab
Vertical tab
Backslash
Single quote
Double quote
Question mark
Chapter 4
• I/O Stream Manipulators
setw(n)
Sets field width to n
setprecision(n) Sets floating-point
precision to n
setfill(n)
Sets fill character to n
ws
Extracts white space characters
endl
Inserts a new line in the output
stream, then
flush
Flushes the output stream
Chapter 4
• Setting the Field Width
cout << setw(field width) << output item;
Chapter 4
• Generating Decimal Point Values
cout.setf(ios::fixed );
cout.precision(n);
Chapter 4
• Generating Left-Justified Values
cout.setf(ios::fixed);
cout.setf( ios::left);
Chapter 4
• Generating Currency Values
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
Chapter 4
• Formatting Functions and Flags
setf()
unsetf()
precision(n)
fixed
left
right
scientific
showpoint
showpos
Sets a formatting flag.
Unsets a formatting flag.
Sets decimal output to n decimal places.
Forces fixed decimal point output.
Forces left justification within field.
Forces right justification within field.
Forces exponential, e, output.
Forces a decimal point to be displayed along
with trailing 0’s.(Used for currency outputs)
Forces a + sign output for positive values.
Chapter 4
• The cin Object
The cin object is used to read information
from the keyboard and is contained in the
iostream.h and iostream header files.
Chapter 4
• The cin Input Stream Flows from the
Keyboard
74
92
cin
88
CRLF
Input Stream
blank character
74 92 88 
Key board
Chapter 4
• Using the cin Object
cin >> variable to be read;
– Only one character is read at a time.
– White space (blanks, tabs, new lines, carriage
returns, etc.) are ignored by cin when using the
>> operator. However, white space can be read
using different cin functions.
– Numeric values can be read as characters, but
each digit is read as a separate character.
Chapter 4
• Using get() and put() For Single
Character Data
cin.get(character variable);
cout.put(character variable);
Chapter 4
• Reading Strings Using getline()
getline(input stream, string object,
'delimiting
character’);
– To avoid a potential problem, use the line
cin >> ws before the cin.getline() statement.
Chapter 4
• Opening an Input File
ifstream fin(”file path");
• Opening an Output File
ofstream fout(”file path");
• Opening an Input/Output File
fstream finout(“file path”);
Chapter 4
• Processing an Open File
if(!fileObject)
{
cout << "This file cannot be opened" <<
exit(1);
}//END IF
else
{
while (Read Data Item From Input File )
{ //BEGIN LOOP
Process data item.
} //END LOOP
}//END ELSE
endl;