





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The extraction operator '>>' and insertion operator '<<' in c++ for input and output of different data types, including integers, doubles, and strings. It also covers various manipulators and their uses for formatting output. An example program demonstrating the usage of these operators and manipulators is provided.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






The extraction operator >> is used for general input of variable values. The extraction operator is defined for all the built-in basic C++ types. It is possible to input values for multiple variables in the same statement by chaining them together with the extraction operator. For each occurrence of >> you must supply the name of the variable where you want the input value assigned (a statement can’t end with >>). cin >> value1 >> value2; is the same as cin >> value1; cin >> value2; Rules for how C++ reads different types of values: int Skip any leading spaces or blank lines, read digits until encountering a character that is not valid in an integer value, like a blank space or a special character, or a decimal point, or a letter, or the end of the line (result of pressing Enter) double Skip any leading spaces or blank lines, read digits until encountering a character that is not valid in a floating-point value, like a blank space or a special character other than a decimal point, or a letter, or the end of the line. char Skip any leading spaces or blank lines and read the first non-blank character encountered. string Skip any leading spaces or blank lines and read characters until encountering a blank space or the end of the line.
#include
endl Inserts a new-line and flushes the stream (output only) boolalpha Causes boolean values to be input/output as strings true and false noboolalpha Causes boolean values to be input/output as integers 1 and 0 (this is the default) fixed Causes float values to be displayed in fixed decimal format scientific Displays float values in exponential notation left Causes output to be left-justified right Causes output to be right-justified (this is the default) showpoint Shows the decimal point and trailing zeros for float values noshowpoint Suppresses display of decimal point for float values with a zero fractional part Displays trailing zeros for float values with a non-zero fractional part showpos Adds leading + to positive numbers noshowpos Does not add a leading + to positive numbers (this is the default)
setprecision (int p) Sets the number of digits displayed after a decimal point for floats (affects output only if the fractional portion of the number is nonzero) Note: setprecision may not work as expected unless used in combination with both fixed and showpoint manipulators. setw (int w) Sets field width for the next output value only setfill (int ch) Sets fill character (the default is blank)
#include
setw doesn’t also apply to the second asterisk printed cout << '' << left << setw(10) << "HI MOM" << '' << endl; // 2
cout << '' << setw(9) << 100.00 << '' << 123.426879 << ‘*’ << endl; // 3
field width not reused for second value; .426879 rounded to. cout << '' << right << setw(8) << "HI MOM" << '' << endl; // 4
cout << '' << setw(1) << "HI MOM" << '' << endl; // 5
// cout << ‘’ << setw(10) << setprecision(4) << 123.426879 << ‘’ << endl; // 6
the total # of digits to be printed cout << fixed << showpoint ; // these can be included in any output statement or by themselves
.426879 rounded to.
no rounding because first digit lost is less than half
will apply to values printed following the manipulator
but setprecision doesn’t apply to ints – would need typecast
Assume the declaration: char ch;