Basics of Input and Output with C++


New Member
Aug 21, 2017
Best answers
Hello folks! This is your boi Chiha and I will be helping you understand the basics of C++ input and output this is a good place to start if you have never encountered doing this in C++ before. I will assume that you do have some basic understanding of programming though.

We are going to jump right into it with talking about streams. C++ uses an abstraction which is called streams which are used for input and output. In essence a stream is an entity wherethe program can either inset or extract characters to or from. Here are your basic streams, however, for this thread I will only be going over the first two streams.

cout [This is the standard output stream]
cin [This is the standard input stream]
cerr [This is the standard error output stream]
clog [This is the standard logging output stream]


First let's take a look at cout. The proper way to write it is: cout << (the data goes in here);

cout << "Hello world"; //prints the statement "hello world"
cout << 456; //prints the numbers "456"
cout << h; //prints whatever was in "h"

You can also do multiple operations such as:

cout << "Hello " << "my name " << "is Bob.";

this is print: Hello my name is Bob.
You can see how this can be very useful for chaining together a sentence, especially if there is a unknown field that the user will be filling in. Lastly adding a \n will add a line break. If you were to write:

cout << "Hello /nHow are you?';

The result is:

How are you?

This is a great feature for the readability of your messages.


Now let's take a look at cin. The standard input for cin is the keyboard and it will always default to that. To use cin you will need variable to store the data. To declare a variable start by specifying the data type and then the name of the variable:

int height; //A variable called height that stores an integer.

Now you can use the cin to collect data and store it in the variable. Make sure to remember that the the input from the keyboard is only sent to the program once the enter (return) key is hit. The program will wait indefinitely until the enter key it pressed. The notation used for cin is two arrows like this >> So if we wanted to get data and but it into the variable height we would write:

cin >> height

That would collect the data typed by the user and assuming it is a integer it would store it in the variable height. Here is an example program using both cout and cin:

//example program for cin with a int
#include <iostream>
using namespace std;

int main ()
int height;
cout << "Hello, please enter your height: ";
cin >> height;
cout << "Your height is: " << height;
return 0;

As you can see, using cin makes getting data fairly simple. This program is pretty basic but I hope you can see the many ways this can be used. However, there are some problems with cin. If the user enter a String or something that is not an integer the operation will fail. But, for right now cin does the job for us.

Cin with Strings

Before I end this post I do want to touch on using string with cin. Above we saw how to use cin to get a integer. But what if you want to get a string like a name or something? Well we can do that storing it in a string variable. This is done the exact same way as the we did above with the integer. Here is another example program:

//example program for cin with a string
#include <iostream>
using namespace std;

int main ()
string name;
cout << "Hello, please enter your name: ";
cin >> name;
cout << "Hello " << name;
return 0;

This program will ask the user for their name and they say hello. The string is a very useful variable and knowing how to get it from the user is very important.

Hopefully this helped you understand the basics of input and output with C++.
Likes: TopSilver


web developer
Mar 10, 2017
Best answers
I'm familiar with the basics of cin and cout from reading the C++ Bible, a book I bought to get started on the basics. That's as far as I got except for more information I haven't yet absorbed but great article. This should help beginners on the starting principles. Thanks for sharing.
Likes: Chiha