|
Output
 | Standard output is tied to predefined iostream (iostream.h) and is directed to terminal |
 | - Must include iostream.h in order to put values to standard output |
 | Format (two types) |
 | cout << output for output a character, a character string, an identifier or any
combination of these separated by << |
 | NOTE: The output operator << points in the direction of the flow
of the data |
 | a character is enclosed by single quotes; a system directive is treated as a single
character and is a letter preceded by a backslash (\) |
 | '\n' end of line |
 | '\t' tab |
Examples:
| cout << 'a'; cout << '#' << ' '
<< '!' << '\n';
cout << '3' << '\n'; |
Would result in the output
 | A character string is enclosed by double quotes |
 | cout << "hello world\n" |
 | The output statements above could be written as |
 | cout << "a# !\n3\n"; |
 | An output statement may include an identifier alone or in combination with other types
of output |
 | cout << "The total is " << total << '\n'; |
Endl vs '\n'
| cout << "test\n"; cout <<
"test" << endl; |
Would result in same output. BUT endl flushes the output buffer
- cout.put (ch)
for ch a single character with single quotes or a char identifier
Examples
| cout.put ('a'); char letter = 'a');
cout.put (letter); |
NOTE: cout.put (<id>) is used for single character output. The
ostream operator may be used for any type of output.
Example
| #include <iostream.h> main () {
cout << "Hello" << endl;
cout << "Hello\n";
cout << "H";
cout << "ello" << endl;
cout.put ('H');
cout.put ('e');
cout.put ('l');
cout.put ('l');
cout.put ('o');
cout.put ('\n');
} |
Output:
Formatting numerical output
 | Formatting is done by system defined functions; must include iomanip.h in order to use
these functions |
 | setw (fieldwidth): sets field width to value specified |
 | Right justified |
 | If width is greater than specified, all are printed |
 | Float values |
 | setiosflags(specifications) |
 | For specifications |
 | ios::fixed |
 | ios::showpoint |
 | ios::right |
 | ios::left |
 | Separate specifications with | |
 | setprecision (decplaces): for decplaces the number of places to right of the decimal
point |
Example
| cout << setw(10) << setiosflags(ios::fixed
| ios::showpoint) << setprecision (2) << average << endl; |
Note: See pages 88-89 of Deitel and Deitel for more information
Input
- Standard input is also tied to predefined iostream <iostream.h>. Usually
from the keyboard.
Format
 | cin >> inputlist |
 | for inputlist an identifier or a list of identifiers separated by >> |
 | NOTE: The input operator >> points in the direction of the flow of the data |
Example
| int numitems; float g1, g2, g3;
char ch;
cin >> numitems;
cin >> g1 >> g2 >> g3 >> ch; |
Input list is read IN ORDER SPECIFIED
 | For character data, scans whitespace |
 | cin.get (ch) |
 | for ch a character identifier |
Example
| char letter, ch; cin.get (letter);
cin.get (ch); |
This example:
 | Reads any character--blank, end of line, etc. |
 | Assignment of value |
 | Assigns value within program |
|