



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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: Programming Fundamentals II; Subject: Computer Science - CSCI; University: Texas A & M University-Commerce; Term: Unknown 2011;
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!
The insertion operator << is used for general output of values of multiple types. It has been defined to output values in a default format for all of the built-in basic C++ types. Because of the way the operator is defined, it is possible to output multiple values in the same statement by chaining them together with the << (insertion) operator: cout << value1 << value2; is the same as cout << value1; cout << value2; where the variables (or constants or expressions) may be of any of the built-n basic types. The insertion operator converts values to text form. By default, values are displayed in a width equal to their size and formatted as follows: char A type char value, if it represents a printable character, is displayed as a character in a field one character wide. int Numeric integer types are displayed as decimal integers in a field just wide enough to hold the number and, if the value is negative, a minus sign. float Floating-point types are displayed with 6 places to the right of the decimal, except that trailing fractional zeros aren't displayed. If there are no significant digits to the right of the decimal point, printing of the decimal point is suppressed along with the trailing zeros. The number may be displayed in E-notation, depending upon the value of the number (which depends on the implementation being used). Again, the field is just wide enough to hold the number and, for a negative value, a minus sign. string Strings are displayed in a field width equal in width to the length of the string. Examples of default field widths (these defaults may vary from one compiler to another): 1 #include
To override any of C++'s default formatting, C++ provides manipulators which have special meaning in the context of input and/or output constructs. Each causes a corresponding library function to be called, which changes the state of the specified stream as requested. To use any of these manipulators, simply insert them within the I/O stream preceding the value they are to manipulate. Manipulators Having No Parameters (defined in iostream.h ) Manipulator Purpose
dec Sets the stream's radix (base) to decimal (default) endl Inserts a new-line and flushes the stream (output only) (By default, output streams are buffered, so use flush or endl to write out immediately) ends Inserts a null character (output only) flush Flushes the stream (output only) hex Sets the stream's radix (base) to hexadecimal oct Sets the stream's radix (base) to octal ws Skips leading white space on input (default) The following manipulators were added as part of the new ANSI standard: boolalpha Causes boolean values to be input/output as strings true and false instead of 1 and 0 noboolalpha Causes boolean values to be input/output as integers 1 and 0 fixed Causes float values to be displayed in fixed decimal format scientific Displays float values in exponential notation internal Pads after sign or base indicator to fill specified field width left Causes output to be left-justified right Causes output to be right-justified (default) showbase Shows the base for octal (0) and hexadecimal (Ox) numbers noshowbase Does not show the base for hexadecimal and octal output 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 show + for positive numbers (default) skipws Skips leading whitespace on input (default) noskipws Does not skip leading whitespace on input unitbuf Flushes the output buffer after each insertion operation nounitbuf Does not flush output buffer after each insertion operation uppercase Shows hexadecimal numbers and exponent in uppercase nouppercase Displays hexadecimal numbers and exponents in lower case If your program includes the statement using namespace std; the preceding manipulators may be inserted directly into the output stream. Without the namespace statement, they can only be used with the setiosflags and resetiosflags manipulators below. See the sample program on p. 3 for examples of using them in a program. Manipulators Having One Parameter (defined in iomanip.h ) Manipulator Purpose
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) setw (int w) Sets field width for next output value only setiosflags (long f) Sets format state flags specified in f resetiosflags (long f) Turn off (reset to default) the format state flags specified in f setbase (int base) Sets number base to 8, 10, or 16 (default is 10) setfill (int ch) Sets fill character (default is blank) The general-purpose manipulators setiosflags and resetiosflags have a parameter of a set of bit-flags. Each open stream maintains a series of bit-flags. Each flag records whether a particular attribute is or is not set. This set of flags records the stream's format state. In iostream.h, an enumerated type is defined with a set of enumeration constant names for each format state.
The extraction operator >> is used for general input of values of multiple types. The extraction operator is defined for all the built-in basic C++ types. As with the insertion operator, it is possible to input values for multiple variables in the same statement by chaining them together with the extraction operator: cin >> value1 >> value2; is the same as cin >> value1; cin >> value2; To input a numeric value, leading whitespace is skipped and then characters are input until a character inappropriate for the type is encountered. The default input base is decimal (numbers without an explicit base are assumed to be decimal), but it is possible to input octal or hexadecimal values by explicitly entering the base: 024 would be read as an octal 24 (decimal 20) and 0x14 would be read as a hexadecimal 14 (decimal 20). Leading whitespace is also skipped when characters and strings are input. When a value is to be read for a char variable, the first non-whitespace character is input. When a value is to be read for a string variable, characters are read starting with the first non-whitespace character until whitespace (end-of-line, blank, tab, end-of-file) is encountered.
istream & get (char & c); inputs the next character ( even if it is whitespace ), storing the input character in the char parameter. This form of the function returns 0 (false) if end of file is encountered before a character can be input. Example: cin.get(ch); int get (void); inputs the next character ( even if it is whitespace ), returning the character input (or returning EOF if end of file -- EOF is a predefined constant for the end-of-file symbol) Example: ch = cin.get(); or while ( ( ch = cin.get() ) != '\n') ... // read chars across a line until the end-of-line marker is found Examples of reading chars until the end-of-line marker (‘\n’) is found (assume the declaration: char ch): (a) (b) (c) cin >> ch; cin.get(ch); while (ch != '\n') while (ch != '\n') while ( ( ch = cin.get() ) != '\n') { { cout << ch; cout << ch; cout << ch; cin >> ch; cin.get(ch); } } Input for each program is the 7 characters Hi Mom! followed by the enter key ( \n symbol ). Output for each program: (a) b) (c) HiMom! Hi Mom! Hi Mom! (continues in an infinite loop) istream & ignore (int len = 1, int delim = EOF); skips over a designated number of characters (first parameter; default is 1) or terminates upon encountering a designated delimiter (second parameter; default is EOF, which causes ignore() to skip to the end of file when reading from a file). The delimiter is skipped. Example: cin.ignore(); or cin.ignore(2); or cin.ignore(80,'\n'); ostream & put (char c); outputs one character (the parameter supplied) Example: cout.put('\a'); (make computer beep) or cout.put('A'); ostream & put (int n); outputs the char equivalent of the parameter (assumed to be an ASCII number) Example: cout.put(7); (make computer beep) or cout.put(65); (display ‘A’ ) int peek (void); returns next character in the input stream, but does not remove the character from the stream (that character remains to be read). Example: ch = cin.peek(); or while ( ( ch = cin.peek() ) != ' ') ... or while (cin.peek() != '\n') int eof (void); returns true if end of file has been reached, or returns false otherwise Example: while (! cin.eof() ) ... (you’d have to enter the end-of-file symbol from the keyboard
-- for DOS it’s Ctrl Z, for Mac and UNIX/Linux it’s Ctrl D )