Download presentation
Presentation is loading. Please wait.
Published byΚλεοπάτρα Βαρουξής Modified over 6 years ago
1
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
2
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
3
cin – input stream Defined in the iostream library cin
// 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; } 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!
4
cout – output stream Defined in the iostream library cout
// 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; } 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!
5
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 non- white-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
6
Input Stream Example $ John $ 2.3 $ User types this note: $ is the prompt loads buffer… Stream Buffer Pointer J o h n space 2 3 6 . 1 \n 4 5 7 8 9 10 11 12 13 14 15 16 User starts typing, Stream Buffer is loaded until: “Enter” is typed (‘\n’), at position 12: Pointer starts at 1, “John” gets stored in name - stops at 5 Pointer skips over space in 5, 23 gets stored in i - stops at 8 Pointer skips 8, 6.1 gets stored in x - stops at 12 More typing, buffer loads until “Enter” is typed again (16) Pointer skips over 12, 2 gets stored in j - stops at 14 … because an int (j) cannot contain a decimal point! Pointer continues from 14, .3 gets stored in y - stops at 16 string name; int i, j; float x, y; cin >> name; cin >> i >> x >> j >> y;
7
getline() getline(stream, string_variable);
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!!! // string variable string name; // read an entire // line into “name” getline(cin, name);
8
Output Stream By default, cout outputs data with no formatting Strings
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
9
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
10
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
11
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>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.