DEV Community

Cover image for Stream Input and Output
Saloni Goyal
Saloni Goyal

Posted on • Edited on

Stream Input and Output

Stream I/O

The Standard Library provides us with the functionality for input/output (I/O) where input and output can be -

  • keyboard/screen
  • files - read from a file and write to another (or the same file)
  • other sources/targets that support stream I/O

Stream I/O is different from "record" I/O or "fixed" I/O as in it cannot be used to -

  • create a database, or
  • read one line in the middle of a file

C++ supports stream I/O with operators. Operators in programming are similar to what we see in mathematics, for example in 2 + 2, '+' is the operator adding the two operands.

  • To send character to cout (pronounced c-out), use the << (from) operator.

std::cout << "Hello!";

  • For line break (or enter) use the escape sequence '\n'.

std::cout << "Hello!\n";

is same as writing

std::cout << "Hello!" << '\n';

Comments

Comments in code help the reader of your code understand why some things in your code are as the way they are. You may also be reading your code again after a few days, weeks or months and comments help in understanding why some logical decision was made.

To add a single line comment in C++, use two forward slashes (//)

// This is a comment

Alt Text

  • Double and single quotes are different

Don't ask for the impossible

  • 3/0
  • "hello" + 2

  • Also remember to save your changes before you run :)

Now, try to write a program that prints out a combination of words and numbers across multiple lines.

Please leave out comments with anything you don't understand or would like for me to improve upon.

Thanks for reading!

Top comments (0)