Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Basic I/O

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 the std namespace. To use the std namespace, we used the using namespace std; statement.
  • Every C++ program starts with the main() function. The codeexecution begins from the main() 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 the main() 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
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!