Introduction
Now that you’ve had a quick introduction to data and variables, we'll go over how the basic input and output of your programs will work, so that you'll be able to experiment with the next few concepts you learn. Console input and output is very simple to use in C++, and we’ll be using it for quite a while.
Data Streams
The abstraction of "data streams" is used to understand the flow of data into and out of a program. A data stream is considered to be one path data can take within your computer and peripheral devices. You can think of a data stream as, well, a stream: data flows along this data stream; your program can put data into it, or take data out of it.
The two streams you will use at first are considered to be the default I/O or input and output streams. That is to say, they are the ones to be used if no other options are chosen. You will use the keyboard, which is considered to be the "Console In" device, and you will use the monitor (as a text-based console), which is considered to be the "Console Out" device. These terms are actually integrated into C++: the keyboard input device is represented by an object called cin ("console-in"), and the console output device will be represented by an object called cout ("console-out").
Operators
The input and output process uses operators to indicate the flow of data. Using these operators assumes that your metaphorical data stream is on the left side of your screen, and the data source or destination is to the right. The insertion operator, used for output, hence points to the left side of your screen, as if it is inserting data into the data stream. The extraction operator, used for input, points to the left, as if it is extracting data from the data stream. We will learn how to use operators in more complex ways in future lessons.
Cout and Cin
To use these two data streams, you will need to include the header "iostream" in your program. "iostream" is a part of the C++ standard library, and provides the "cout" and "cin" objects, as well as some other useful features. You can also include the line "using namespace std;" which tells your program to automatically pull "cout" and "cin" from the "std" (standard) namespace. If you omit this line, you must always type "std::cout" and "std::cin."
#include <iostream> using namespace std;
For console output, you will use the data stream with the identifier "cout." As mentioned previously, you then write the insertion operator, "<<," followed by the data you want to send to the console. You can then add another insertion operator followed by more data, and so on. Finally, you can also send "endl" to the console, which will simulate pressing enter, ending the current line and creating a new one.
cout << "Hello World" << endl; cout << "This demonstrates" << " sending text to the console.";
Using console input it is much the same as output, except you use the extraction operator, ">>." On the right of your extraction operator, you place the variable you want to capture the input.
cin >> userAge; cin >> character;
Escape Characters
Quick note: if you want to include a reserved character (such as a double quote) in a string, first type a backslash, and then the character.
"The person said \"hello\" to me"
This tells the compiler to include that character in the string, and the slash won’t show up. You also have to use this for backslashes themselves (i.e. “\\” to output one slash).
Holding the Screen
If you run your program with no way to hold the screen, it will open and immediately close, not allowing the user to see your output. To display the message “Press any key to continue...” and wait for the user to press a key before continuing, use the line
system("pause");
Note that this only works on Windows operating systems.