Formatted I/O in C++
C++ helps you to format the I/O operations like determining the number of digits to be displayed after the decimal point, specifying number base etc.
stream.setf(ios::showpos) If input=100, output will be +100
stream.setf(ios::showpoint) If input=100.0, output will be 100.000
Note: Here, stream is referred to the streams defined in c++ like cin, cout, cerr, clog.
There are two ways to do so:
- Using the ios class or various ios member functions.
- Using manipulators(special functions)
- width(): The width method is used to set the required field width. The output will be displayed in the given width
- precision(): The precision method is used to set the number of the decimal point to a float value
- fill(): The fill method is used to set a character to fill in the blank space of a field
- setf(): The setf method is used to set various flags for formatting output
- unsetf(): The unsetf method is used To remove the flag setting
-------------------------- Implementing ios::width A 10 -------------------------- -------------------------- Implementing ios::fill *********a ****1 -------------------------- -------------------------- Implementing ios::setf +100 +200 -------------------------- -------------------------- Implementing ios::unsetf 200.000 --------------------------
- boolalpha: The boolalpha manipulator of stream manipulators in C++ is used to turn on bool alpha flag
- dec: The dec manipulator of stream manipulators in C++ is used to turn on the dec flag
- endl: The endl manipulator of stream manipulators in C++ is used to Output a newline character.
- and: The and manipulator of stream manipulators in C++ is used to Flush the stream
- ends: The ends manipulator of stream manipulators in C++ is used to Output a null
- fixed: The fixed manipulator of stream manipulators in C++ is used to Turns on the fixed flag
- flush: The flush manipulator of stream manipulators in C++ is used to Flush a stream
- hex: The hex manipulator of stream manipulators in C++ is used to Turns on hex flag
- internal: The internal manipulator of stream manipulators in C++ is used to Turns on internal flag
- left: The left manipulator of stream manipulators in C++ is used to Turns on the left flag
- noboolalpha: The noboolalpha manipulator of stream manipulators in C++ is used to Turns off bool alpha flag
- noshowbase: The noshowbase manipulator of stream manipulators in C++ is used to Turns off showcase flag
- noshowpoint: The noshowpoint manipulator of stream manipulators in C++ is used to Turns off show point flag
- noshowpos: The noshowpos manipulator of stream manipulators in C++ is used to Turns off showpos flag
- noskipws: The noskipws manipulator of stream manipulators in C++ is used to Turns off skipws flag
- nounitbuf: The nounitbuf manipulator of stream manipulators in C++ is used to Turns off the unit buff flag
- nouppercase: The nouppercase manipulator of stream manipulators in C++ is used to Turns off the uppercase flag
- oct: The oct manipulator of stream manipulators in C++ is used to Turns on oct flag
- resetiosflags(fmtflags f): The resetiosflags manipulator of stream manipulators in C++ is used to Turns off the flag specified in f
- right: The right manipulator of stream manipulators in C++ is used to Turns on the right flag
- scientific: The scientific manipulator of stream manipulators in C++ is used to Turns on scientific flag
- setbase(int base): The setbase manipulator of stream manipulators in C++ is used to Set the number base to base
- setfill(int ch): The setfill manipulator of stream manipulators in C++ is used to Set the fill character to ch
- setiosflags(fmtflags f): The setiosflags manipulator of stream manipulators in C++ is used to Turns on the flag specified in f
- setprecision(int p): The setprecision manipulator of stream manipulators in C++ is used to Set the number of digits of precision
- setw(int w): The setw manipulator of stream manipulators in C++ is used to Set the field width to w
- showbase: The showbase manipulator of stream manipulators in C++ is used to Turns on showbase flag
- showpoint: The showpoint manipulator of stream manipulators in C++ is used to Turns on show point flag
- showpos: The showpos manipulator of stream manipulators in C++ is used to Turns on showpos flag
- skipws: The skipws manipulator of stream manipulators in C++ is used to Turns on skipws flag
- unitbuf: The unitbuf manipulator of stream manipulators in C++ is used to turn on unitbuf flag
- uppercase: The uppercase manipulator of stream manipulators in C++ is used to turn on the uppercase flag
- ws: The ws manipulator of stream manipulators in C++ is used to skip leading white space
To access manipulators that take parameters (such as setw( )), you must include “iomanip” header file in your program.
9. Formatted Inputs and Outputs¶
In Listing 5.1 , we used ‘%4d’ and ‘%21.5’ etc. to print the outputs in readable format. In this chapter, we will some more commands for formatting input and outputs i.e. ‘scanf’ and ‘printf’ commands respectively.
9.2. Printing Integer and Floating point values¶
Table 9.1 shows the list of ‘conversion specifier’ e.g. ‘d’ and ‘f’ etc. for printing ‘Integer’ and ‘Floating point’ values. Further, these specifiers are used to print the values in different formats in Listing 9.1 .
Integer Format | Description |
---|---|
d or i | Signed decimal integer |
o | Unsigned Octal integer |
u | Unsigned decimal integer |
x or X | Unsigned Hexadecimal integer |
Floating Point Format | Description |
f | Fixed notation (e.g. 102.300003) |
e or E | Exponential notation (e.g. 1.023000e+002) |
g or G | Display in format ‘f’ or ‘e’ based on value |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// printIntFloat.c #include int main() int i = 10, j = -12; float x = 102.3, y=-23.49; printf("----- Integer format ----- \n"); printf("%d, %i, %o, %u, %x \n", i, i, i, i, i); // hex and oct : -ve numbers are displayed as "2's complement" printf("%d, %i, %o, %x\n\n", j, j, j, j); printf("----- Floating point format ----- \n"); printf("%e, %f, %g \n", x, x, x); printf("%e, %f, %g \n", y, y, y); // hex and oct : -ve numbers are displayed as "2's complement" // printf("%d, %i, %o, %x ", y, y, y, y); return 0; > /* Outputs ----- Integer format ----- 10, 10, 12, 10, a -12, -12, 37777777764, fffffff4 ----- Floating point format ----- 1.023000e+002, 102.300003, 102.3 -2.349000e+001, -23.490000, -23.49 */
9.3. Modify ‘printf’ format¶
Table 9.2 shows various flags which can be used to format the outputs for the ‘printf’ statement. Listing 9.2 and Listing 9.3 show the usage of these flags. Please read the comments under these listings.
Flag | Description |
---|---|
+ | Right justified |
— | Left justified |
# | Prefix ‘o’ and ‘x’ for octal and hex outputs respectively |
space | Prints space before positive values |
0 | Pad leading zeros |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
// printfFlag.c #include int main() int i1 = 210, i2=-15; float j1 = -123.34, j2=12.22; char c1[] = "Meher", c2[] = "Krishna"; // 10d : 10 is the minimum width for integer // 10.4f : 10 and 4 are minimum width for integer part and float part respectively printf("---------- Right justified ---------- \n"); printf("%10d %10.4f %10s\n", i1, j1, c1); printf("%10d %10.4f %10s\n\n", i2, j2, c2); // +10d to add `+ sign' with numeric values printf("%+10d %+10.4f %10s\n", i1, j1, c1); printf("%+10d %+10.4f %10s\n\n", i2, j2, c2); // 0 is used for paddign zeros // 0 can not be used with 'left justified' printf("%010d %010.4f %10s\n", i1, j1, c1); printf("%010d %010.4f %10s\n\n", i2, j2, c2); // - for left justified printf("---------- Left justified ---------- \n"); printf("%-10d %-10.4f %-10s\n", i1, j1, c1); printf("%-10d %-10.4f %-10s\n\n", i2, j2, c2); // - for left justified and then + to add `+ sign' with numeric values printf("%-+10d %-+10.4f %-10s\n", i1, j1, c1); printf("%-+10d %-+10.4f %-10s\n\n", i2, j2, c2); // - for left justified and then 'space' to add `space' for +ve values printf("%- 10d %- 10.4f %-10s\n", i1, j1, c1); printf("%- 10d %- 10.4f %-10s\n", i2, j2, c2); return 0; > /* Outputs ---------- Right justified ---------- 210 -123.3400 Meher -15 12.2200 Krishna +210 -123.3400 Meher -15 +12.2200 Krishna 0000000210 -0123.3400 Meher -000000015 00012.2200 Krishna ---------- Left justified ---------- 210 -123.3400 Meher -15 12.2200 Krishna +210 -123.3400 Meher -15 +12.2200 Krishna 210 -123.3400 Meher -15 12.2200 Krishna */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// printfPrefix.c #include int main() int i1=10; // # to print '0x' and '0' before hex and oct numbers respectively printf("---------- Right justified ---------- \n"); printf("%10d %10x %10o\n", i1, i1, i1); printf("%10d %#10x %#10o\n\n", i1, i1, i1); printf("---------- Left justified ---------- \n"); printf("%-10d %-10x %-10o\n", i1, i1, i1); printf("%-10d %-#10x %-#10o\n\n", i1, i1, i1); return 0; > /* Outputs ---------- Right justified ---------- 10 a 12 10 0xa 012 ---------- Left justified ---------- 10 a 12 10 0xa 012 */
9.4. Formatted input using ‘Scanf’¶
In this section, we will see various methods to get inputs from user with ‘scanf’ command.
9.4.1. Format specifiers¶
We can use all the formats specified in Table 9.1 with ‘scanf’ as well. The difference is in ‘d’ and ‘i’ specifiers i.e. ‘d’ can only read integer format, where ‘i’ can read ‘Octal’ and ‘Hexadecimal’ formats as well, as shown in Listing 9.4 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// scanfEx.c #include int main() int a, b, c, d; unsigned int e; // for hexadecimal values (int will give warning) // %d can not read Oct and Hex values // %i can read Oct and Hex values printf("Enter 5 numbers numbers : \n"); scanf("%d%i%i%i%x", &a, &b, &c, &d, &e); printf("Outputs\n"); printf("%d %d %d %d %d", a, b, c, d, e); return 0; > /* Outputs Enter 5 numbers numbers : -12 -12 014 0xc 0xc 014 : Oct input; 0xc : Hex input Outputs -12 -12 12 12 12 */
9.4.2. Scan and rejection set¶
We can select or reject data from the input provided by user, as shown in Listing 9.5 and Listing 9.6 respectively. The ‘[ abc ]’ is used for creating the scan-set, whereas \(\hat abc\) is used for rejection-set. Please see comments in the listing for better understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// scanSetEx.c #include int main() char a[20]; printf("Write Yes, No, Yyes, YNo : "); // read the string starts from 'y, Y, n or N' // and select string till only these letters appear scanf("%[yYnN]", a); printf("Your answer is : %s\n", a); return 0; > /* Outputs Write Yes, No, Yyes, YNo : Yes Your answer is : Y Write Yes, No, Yyes, YNo : YNo Your answer is : YN */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// invertedScanSetEx.c #include int main() char a[20]; printf("Data will be read till Y or Y appears \n"); printf("Enter string : "); // read the string till 'y or Y' appear scanf("%[^yY]", a); printf("You entered : %s\n", a); return 0; > /* Outputs Data will be read till Y or Y appears Enter string : Meher Krishna y You entered : Meher Krishna Data will be read till Y or Y appears Enter string : Meher Krishna y Bye Bye You entered : Meher Krishna */
9.4.3. Suppression character ‘*’¶
Suppression character can be used for removing certain characters while reading input e.g. while reading date in ‘dd-mm-yyyy’, we can skip the ‘-’ as shown in Listing 9.7 , where two methods are shown for suppressing the characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// suppressCharEx.c #include int main() int date, month, year; // define specific skip character printf("Enter data in dd-mm+yyyy format : "); scanf("%d-%d+%d", &date, &month, &year); // skip ' - ' and + printf("Date = %d, Month = %d, Year = %d\n\n", date, month, year); // define general skip character printf("Enter data in dd-mm-yyyy format : "); // %*c read character and skip it scanf("%d%*c%d%*c%d", &date, &month, &year); printf("Date = %d, Month = %d, Year = %d\n", date, month, year); return 0; > /* Outputs Enter data in dd-mm+yyyy format : 12-12+2012 Date = 12, Month = 12, Year = 2012 Enter data in dd-mm-yyyy format : 12*12/2012 Date = 12, Month = 12, Year = 2012 */
9.5. Conclusion¶
In this section, we saw various methods to format the input and output data using ‘scanf’ and ‘printf’ command respectively.
© Copyright 2017, Meher Krishna Patel. Revision 45ed5924 .
Versions latest Downloads pdf html epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.