C++ Standard I/O – cin and cout
In this tutorial we will take a look at basic input and output. Using the C++ iostream library we will get the user’s input from the keyboard and we will print messages onto the screen. The iostream library is part of the C++ standard library.
In C++, I/O is performed by using streams. A stream is a “stream of data” in which character sequences are “flow into” or “flow out off.” A stream is an object with properties that are defined by a class. Global objects are predefined for the standard I/O channels.
The header file iostream must be included to make use of the input/output (cin/cout) operators.
Standard Output (cout)
By default, the standard output of a program points at the screen. So with the cout operator and the “insertion” operator (<<) you can print a message onto the screen. Let’s take a look at an example:
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Note:the double quotes around Hello World (because it is a constant string of characters.)
To print the content of a variable the double quotes are not used. Take a look at an example:
#include<iostream>
using namespace std;
int main()
{
char Yes = ‘y';
cout << Yes;
return 0;
}
Note: If you don’t want to use the namespace std, you could write std::cout << Yes
The << operator can be used multiple times in a single statement. Take a look at an example:
#include<iostream>
using namespace std;
int main()
{
cout << "Hello, " << "this is a test " << "string.";
return 0;
}
Note: the use of the white spaces. (Otherwise the sentence: “Hello, this is a test string.” will be printed like this “Hello,this is a test string.”.)
It is possible to combine variables and text:
#include<iostream>
using namespace std;
int main()
{
char Yes = ‘y';
cout << "Print the character " << Yes;
return 0;
}
The cout operator does not put a line break at the end of the output. So if you want to print two sentences you will have to use the new-line character ( \n ).
For example:
cout << "This is one sentence.\n";
cout << "This is another.\n";
It is possible to use the endl manipulator instead of the new-line character.
For example:
cout << "This is one sentence." << endl;
cout << "This is another." << endl;
The endl manipulator will place a new-line character, so the result is the same. The difference is that the endl manipulator will also flush the buffer when you are in buffered mode. (In most cases the cout will be an unbuffered stream, so you could use the \n or endl, without any difference in behavior.)
Standard input (cin)
In most cases the standard input device is the keyboard. With the cin and >> operators it is possible to read input from the keyboard.
Take a look at an example:
#include<iostream>
using namespace std;
int main()
{
char MY_CHAR;
cout << "Press a character and press return: ";
cin >> MY_CHAR;
cout << MY_CHAR;
return 0;
}
Note: The input is processed by cin after the return key is pressed.
The cin operator will always return the variable type that you use with cin. So if you request an integer you will get an integer and so on. This can cause an error when the user of the program does not return the type that you are expecting. (Example: you ask for an integer and you get a string of characters.) Later on we will offer a solution to this problem.
The cin operator is also chainable. For example:
cin >> XX >> YY;
In this case the user must give two input values, that are separated by any valid blank separator (tab, space or new-line).
That’s all for this tutorial.
In the next tutorial we will look at standard I/O and strings.
i am satisfied.
satisfied
satisfied
Is there a flag that can be tested to see if there is a message in the cin buffer? Message present being defined by the end-of-line character (\n) received.
“Satisfied”! Are we talking about a C++ tutorial or something else! don’t write about your personal experiences here 😉