Basic IO
Basic IO
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.
int age;
cin >> age;
• I am _____ years old
•What is your name? _____________________