Basic Input and Output C programs can read

Basic Input and Output • C++ programs can read and write information using streams • A simple input stream accepts typed data from a keyboard • A simple output stream writes data to the terminal Professor John Carelli Kutztown University Computer Science Department

Standard Input/Output Streams • A stream is a sequence of characters • Streams convert internal representations to character streams or vice versa • >> input (extraction) operator • << output (insertion) operator • Any character (printable or not) • A stream has no fixed size Professor John Carelli Kutztown University 2 Computer Science Department

cin – input stream // first. cpp #include <iostream> // for stream IO #include <string> using namespace std; • Defined in the iostream library #include <iostream> • cin • Standard input stream • Accepts typed input from the keyboard • Uses the extraction operator >> • Extraction operator • Load input into a variable cin >> name; • “points” in the direction of the data flow! Professor John Carelli int main() // start of main function { string name; // Get the name cin >> name; // Output the distance in miles. cout << "Hello " << name << endl; return 0; } Kutztown University Computer Science Department

cout – output stream • Defined in the iostream library #include <iostream> • cout • Standard output stream • Outputs to the terminal window • Uses the insertion operator << • Insertion operator • Output to a device (or file) cout << "Hello " << name << endl; • “points” in the direction of the data flow! Professor John Carelli // first. cpp #include <iostream> // for stream IO #include <string> using namespace std; int main() // start of main function { string name; // Get the name cin >> name; // Output the distance in miles. cout << "Hello " << name << endl; return 0; } Kutztown University Computer Science Department

Input Stream, >> Operator • Last character read is tracked with an input stream buffer pointer • Each new input attempt begins at current pointer position • Leading white space (blanks, tab, newline) skipped until first nonwhite-space character is located, then… • For strings, characters are read into string variable until white space is encountered • For numeric data, characters are read into the variable until a character is encountered that is not a valid character for that data type Professor John Carelli Kutztown University 5 Computer Science Department

User types this note: $ is the prompt Input Stream Example $ John 23 6. 1 $ 2. 3 $ loads buffer… Stream Buffer J o h n space 2 3 space 6 . 1 n 2 . 3 n Pointer 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 string name; int i, j; float x, y; cin >> name; cin >> i >> x >> j >> y; Professor John Carelli User starts typing, Stream Buffer is loaded until: “Enter” is typed (‘n’), at position 12: 1. Pointer starts at 1, “John” gets stored in name - stops at 5 2. Pointer skips over space in 5, 23 gets stored in i - stops at 8 3. Pointer skips 8, 6. 1 gets stored in x - stops at 12 More typing, buffer loads until “Enter” is typed again (16) 1. Pointer skips over 12, 2 gets stored in j - stops at 14 … because an int (j) cannot contain a decimal point! 2. Pointer continues from 14, . 3 gets stored in y - stops at 16 Kutztown University Computer Science Department

getline() • An alternate way to get input • Syntax: getline(stream, string_variable); • Reads from a “stream” (like cin) • Loads everything from the current pointer location up to the next delimiter into a string variable • Default delimiter is ‘n’ (end of line) • Does NOT skip leading whitespace!!! Professor John Carelli Kutztown University // string variable string name; // read an entire // line into “name” getline(cin, name); Computer Science Department

Output Stream • By default, cout outputs data with no formatting • It just outputs the characters required to represent the variable • Strings • Output the characters in the string • Integers • Output the digits in the number • Float point numbers • Can vary based on the value, usually a minimal representation of the number • Boolean • By default : 0 for false, 1 for true Professor John Carelli Kutztown University Computer Science Department

Formatting output • Embed spaces between items • Add a “carriage return” to start a new line • Use “endl” to output a c/r • Same as outputting the character ‘n’ cout << “George” << “ “ << “Washington” << endl; • Or use manipulators Professor John Carelli Kutztown University Computer Science Department

Manipulators • Provide greater control over output formatting • Embed them in the output stream cout << fixed << setw(10) << setprecision(3) << x << endl; • Settings persist until changed • Must include the iomanip library #include <iomanip> • setw is an exception • Width resets after a value is output Professor John Carelli Kutztown University Computer Science Department

Output Stream - Manipulators setw Set the width of the next output value. After output the value resets to zero. Zero width defaults to the object width (i. e. no padding) boolalpha noboolalpha Switches between textual and numeric representation of booleans showpoint noshowpoint Controls whether decimal point is always included in floating-point representation skipws noskipws Controls whether leading whitespace is skipped on input left right Sets the placement of fill characters (i. e. right or left justification) fixed scientific Changes formatting used for floating-point I/O endl Outputs 'n' and flushes the output stream setprecision Changes floating-point precision Manipulators require the inclusion of the iomanip library • #include <iomanip> Professor John Carelli Kutztown University Computer Science Department
- Slides: 11