You are on page 1of 2

C++

Chapter 10: Managing Console I/O Operations

Member Example Note


Function
get() char c; Gets a character from the keyboard and
cin.get(c); or assigns it to variable c.
c = cin.get( );
put() cout.put(‘a’); or Displays character a on the monitor.
cout.put(97); or
char c = ‘a’; cout.put(c);
getline() char name[20]; Reads maximum 19 characters or all
cin.getline(name,20); characters up to ‘\n’. ‘\n’ is replaced with
‘\0’. Useful to read series of words.
write() cout.write(name,20); Displays all 20 characters and does not
stop when ‘\0’ is found.
cout.write.(string1,20).write(string2,20) Useful to concatenate two strings &
display it on the monitor.
width() cout.width(5); cout << 182; Displays two blanks and 182. Output is
right justified. Useful for only one item.
Item that follows immediately.
precision() cout.precision(3); Output:
cout << sqrt(2) << endl; 1.141 (truncated)
cout << 3.14159 << endl; 3.142 (rounded to nearest cent)
cout << 2.50032 << endl; 2.5 (no trailing zeros)
precision retains the setting in effect until it
is reset.
fill() char c = ‘*’; cout.fill(c); or Output:
cout.fill(‘*’); * * * * * 12345
cout.width(10); fill stays into effect until it is reset.
cout << 12345;

Formatting flags, bit-fields and setf():

There are two formats mentioned below:


cout.setf(arg1);
cout.setf(arg1,arg2);
st
1 format:
Flag Meaning
ios::showbase Use base indicator on output
ios::showpos Print + before positive numbers
ios::showpoint Show trailing decimal point and zeros
ios::uppercase Use uppercase letters for hex output
ios::skipws Skip white space on input
ios::unitbuf Flush all streams after insertion
ios::stdio Flush stdout and stderr after insertion

2nd format:
The arg1 is one of the formatting flags defined in the class ios. The arg2 known as bit field
specifies the group to which the formatting flag belongs.

1st argument should be one of the group members of the second argument.

By Darshit Shah Page 1 of 2


C++

Flags and bit fields for setf() function


Format Required Flags (arg1) Bit-field (arg2)
Left-justified output ios::left ios::adjustfield
Right-justified output ios::right ios::adjustfield
Padding after sign or base ios::internal ios::adjustfield
indicator (like +##20)

Scientific notation ios::scientific ios::floatfield


Fixed Point notation ios::fixed ios::floatfield

Decimal base ios::dec ios::basefield


Octal base ios::oct ios::basefield
Hexadecimal base ios::hex ios::basefield

The flags set by setf() remain effective until they are reset or unset. It can be reset any number of
times in a program. We can apply more than one format controls jointly on an output value. The
setf() sets the specified flags and leaves others unchanged. i.e. these flags are mutually exclusive
and therefore can be set or cleared independently.

Examples:
cout.fill(‘~’);
cout.setf(ios::left,ios::adjustfield);
cout.width(10);
cout << “VALUE”; gives output:VALUE~~~~~

Managing output with MANIPULATORS:

Always include header file <iomanip.h> for manipulators.

General Form:
cout << manip1 << manip2 << manip3 << variable;
cout << manip1 << variable1 << manip2 << variable2;

Manipulator Equivalent
setw(int w) width()
setprecision(int d) precision()
setfill(int c) fill()
setiosflags(long f) setf()
resetiosflags(long f) unsetf()
endl “\n”

Examples:
cout << setw(10) << 12345;
cout << setw(10) << setiosflags(ios::left) << 12345;
cout << setw(5) << setprecision(2) << 1.2345 << setw(10) << setprecision(4) << sqrt(2) << endl;

By Darshit Shah Page 2 of 2

You might also like