English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Basic Tutorial

C++ Flow Control (Flow Control)

C++ Function (Function)

C++ Array & String (Array & String)

C++ Data Structure (Data Structure)

C++ Class & Object (Class & Object)

C++ Pointer (Pointer)

C++ Inheritance (Inheritance)

C++ STL Tutorial (STL Tutorial)

C++ Map(container)

C++ <cmath>

Input and output

C ++Output

In C ++In this tutorial, we will learn how to use the cin object to obtain input from the user and use the cout object to display output to the user through examples.

Example1String output

#include <iostream>
using namespace std;
int main() {
    // Print a string enclosed in double quotes
    cout << "This is C ++programming";
    return 0;
}

Output Result

This is C ++Programming

How does this program work?

  • We first include the iostream header file that allows for output display.

  • cout object is defined within the std namespace. To use the std namespace, we use the using namespace std; statement.

  • Each C ++Every program starts with the main() function. Code execution starts from the beginning of the main() function.

  • cout is an object that prints strings within the quotes "". It is followed by the << operator.

  • return 0; is the 'exit status' of the main() function. The program ends with this statement, but it is not mandatory.

Example2Numeric and character output

To print numeric and character variables, we use the same cout object, but do not use quotes.

#include <iostream>
using namespace std;
int main() {
    int num1 = 70;
    double num2 = 256.783;
    char ch = 'A';
    cout << num1 << endl;    // Print an integer
    cout << num2 << endl;    // Print a double floating-point number
    cout << "character: " << ch << endl;    // Print a string
    return 0;
}

Output Result

70
256.783
character: A

Note:

  • The function of endl is to wrap, which can be inserted into the output stream and the effect is to insert a newline character '\n' in the output result. This is why each output is displayed on a new line.

  • If we want to print different variables, strings, etc., in a single statement, we can use the << operator multiple times. For example:

cout << "character: " << ch << endl;

C ++Input

In C ++In C, cin retrieves formatted input from the standard input device (such as the keyboard). We use the cin object and the >> operator for input.

Example3Integer input/Output

#include <iostream>
using namespace std;
int main() {
    int num;
    cout << "Enter an integer: ";
    cin >> num;   // Accept input
    cout << "this number is: " << num;
    return 0;
}

Output Result

Enter an integer: 70
This number is: 70

In the program, we have used

cin >> num;

Accept the user's input. The input is stored in the num variable. We use the >> operator and cin for input.

Note:If the using namespace std; statement is not included, std::cin must be used instead of cin.

C ++Accept Multiple Inputs

#include <iostream>
using namespace std;
int main() {
    char a;
    int num;
    cout << "Enter a character and an integer: ";
    cin >> a >> num;
    cout << "Character: " << a << endl;
    cout << "Numbers: " << num;
    return 0;
}

Output Result

Enter a character and an integer: F
23
Character: F
Numbers: 23