0% found this document useful (0 votes)
32 views

Basic IO

This document discusses basic input and output in C++. It explains that streams are used for sequential input and output of characters, and the standard library defines stream objects like cout and cin for standard output and input. cout is used with the insertion operator << to output data to the console, while cin uses the extraction operator >> to input data from the keyboard or other standard input into variables.

Uploaded by

Mariel Torres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Basic IO

This document discusses basic input and output in C++. It explains that streams are used for sequential input and output of characters, and the standard library defines stream objects like cout and cin for standard output and input. cout is used with the insertion operator << to output data to the console, while cin uses the extraction operator >> to input data from the keyboard or other standard input into variables.

Uploaded by

Mariel Torres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

BASIC INPUT/OUTPUT

Basic of C++
C++ Program Template
• A stream is an entity where a
program can either insert or extract
characters to/from. There is no
need to know details about the
media associated to the stream or
any of its internal specifications.
•All we need to know is that streams are
a source/destination of characters,
and that these characters are
provided/accepted sequentially (i.e.,
one after another).
• The standard library defines a handful of stream objects that can be
used to access what are considered the standard sources and
destinations of characters by the environment where the program runs:
Standard output (cout)
• For formatted output operations, cout is used together with the
insertion operator, which is written as << (i.e., two "less than"
signs).
• The << operator inserts the data that follows it into the
stream that precedes it. In the examples above, it inserted
the literal string Output sentence, the number 120, and the
value of variable x into the standard output stream cout.
Notice that the sentence in the first statement is enclosed in
double quotes (") because it is a string literal, while in the
last one, x is not. The double quoting is what makes the
difference; when the text is enclosed between them, the
text is printed literally; when they are not, the text is
interpreted as the identifier of a variable, and its value is
printed instead. For example, these two sentences have
very different results:
•Multiple insertion operations (<<) may be chained
in a single statement:
• Insert a line break, a new-line character shall be inserted at the
exact position the line should be broken. In C++, a new-line
character can be specified as \n .For example:
• A special symbol called endl (END-of-Line) can
be used to produce a newline. Whenever an
endl is printed, there is no visible output, but the
cursor advances to the beginning (left-margin)
of the next line.
•hello world, again!
•hello,
•one more time.
•543 2.2 1.1
•Beside the endl, you can also use '\n',
which denotes a newline character.

cout << "hello world, again!\n";


• Similarly, you could use '\t', which denote a tab
character, to advance the cursor to the next tab
position.

cout << "\thello,\none\tmore\ttime.\n";


hello world, again!
hello,
one more time.
• Notes: I strongly recommend that you use endl to print a
newline, instead of '\n'. This is because line delimiter is
system dependent: Windows use "\r\n"; UNIX/Linux/Mac
use '\n'. The endl produces system-specific newline.
Furthermore, endl guarantees that the output is flushed;
while '\n' does not.
•cout << “8-Hyacinth" << endl;
Your Name
Section
Standard input (cin)
In most program environments, the standard input by default is
the keyboard, and the C++ stream object defined to access it is
cin.

For formatted input operations, cin is used together with the


extraction operator, which is written as >> (i.e., two "greater
than" signs). This operator is then followed by the variable where
the extracted data is stored. For example:

int age;
cin >> age;
• I am _____ years old
•What is your name? _____________________

You might also like