C++ Basic Input/Output
In this tutorial, we will learn about basic input/output in C++ to use the cin object to take input from the user, and the cout object to display output to the user with the help of examples.
Basic Input/Output
C++ comes with libraries that provide us with many ways for performing input and output functions. In C++ Input and Output are performed in the form of a sequence of bytes or more commonly known as "streams".
1) "Input Stream" :- If the direction of flow of bytes is from the device (like- keyboard, a disk drive, etc) to the main memory then this process is called input.
2) "Output Stream" :- If the direction of flow of bytes is opposite, i.e. from main memory to device (display screen, a printer, etc) then this process is called output.
C++ Output
The C++ cout
statement is the instance of the ostream class. In C++, cout
sends formatted output to standard output devices, such as the screen. We use the cout
object along with the <<
operator for displaying output.
Example 1: String Output
#include <iostream>
using namespace std;
int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming Example1: String Output";
return 0;
}
Output
This is C++ Programming Example1: String Output
How does this program work?
- We first include the
iostream
header file that allows us to display output. - The
cout
object is defined inside thestd
namespace. To use thestd
namespace, we used theusing namespace std;
statement. - Every C++ program starts with the
main()
function. The codeexecution begins from themain()
function. cout
is an object that prints the string inside quotation marks" "
. It is followed by the<<
operator.return 0;
is the "exit status" of themain()
function. The program ends with this statement, however, this statement is not mandatory, but it is good practice for better programming.
Note: 1) If we don't include the using namespace std;
statement, we need to use std::cout
instead of cout
.
2) This is the preferred method as using the std
namespace can
create potential problems.
However, we have used the std
namespace in our tutorials in order to make the codes more readable.
#include <iostream>
int main() {
// prints the string enclosed in double quotes
std::cout << "This is C++ Programming Example1: String Output with std namespace.";
return 0;
}
Output
This is C++ Programming Example1: String Output with std namespace.
Example 2: Numbers and Characters Output
To print the numbers and character variables, we use the same cout
object but without using quotation marks.
#include <iostream>
using namespace std;
int main() {
int num1 = 20;
double num2 = 126.181;
char ch = 'A';
cout << num1 << endl; // print integer
cout << num2 << endl; // print double
cout << "character: " << ch << endl; // print char
return 0;
}
Output
20 126.181 character: A
Notes:
1) The endl
manipulator is used to insert a new line. That's why each output is displayed in a new line.
2) The <<
operator can be used more than once if we want to print different variables, strings and so on in a single statement.
For example:
cout << "character: " << ch << endl;
C++ Input
1) C++ cin
statement is the instance of the class istream and is used to read input device.
2) In C++, cin
takes formatted input from standard input devices such as the keyboard.
3) We use the cin
object along with the >>
operator for taking input.
Example 3: Integer Input/Output
Example To take input from the user
#include <iostream>
using namespace std
int main() {
int age;
cout << "Enter an age: ";
cin >> age; // Taking input from the user.
cout << "The age is: " << age;
return 0;
}
Output
Enter an age: 20 The age is: 20
In the program, we used
cin >> age
to take input from the user. The input is stored in the variable age. We use the >>
operator with cin
to take input.
Note: If we don't include the using namespace std;
statement, we need to use std::cin
instead of cin
.
Example 4: C++ Taking Multiple Inputs
Take multiple input from the user
#include <iostream>
using namespace std;
int main() {
char a;
int age;
cout << "Enter a character and an integer: ";
cin >> a >> age;
cout << "Character: " << a << endl;
cout << "Age: " << age;
return 0;
}
Output
Enter a character and an integer: A 20 Character: A Number: 20
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Basic Input/Output in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Type Conversion
.