in , ,

C++ Basic Input/Output

C++ Basic Input/Output
C++ Basic Input/Output

C++ Basic Input/Output

C++ Input/Output

In this tutorial, we will learn to use the cin item to take input from the user, and the cout object to show output to the user with the help of examples.

C++ Output

In C++, cout sends formatted output to standard output gadgets, for example, the screen. We use the cout object alongside the << administrator for showing output.


Example 1: String Output

#include <iostream>
using namespace std;

int main() {
    // prints the string enclosed in double quotes
    cout << "This is C++ Programming";
    return 0;
}

Output

This is C++ Programming

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 code execution begins from the start of 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.

Note: If we don’t include the using namespace std; statement, we need to use std::cout instead of cout.

#include <iostream>

int main() {
    // prints the string enclosed in double quotes
    std::cout << "This is C++ Programming";
    return 0;
}

Example 2: Numbers and Characters Output

To print the numbers and character factors, we use the equivalent cout object however without using marks

.

#include <iostream>
using namespace std;

int main() {
    int num1 = 70;
    double num2 = 256.783;
    char ch = 'A';

    cout << num1 << endl;    // print integer
    cout << num2 << endl;    // print double
    cout << "character: " << ch << endl;    // print char
    return 0;
}

Output

70
256.783
character: A

Notes:

The endl controller is used to insert another line. That is the reason each yield is shown in another line.

The << operator can be used more than once on the off chance that we need to print various variables, strings, etc in a solitary proclamation. For instance:

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

C++ Input

In C++, cin takes an arranged contribution from standard info gadgets, for example, the console. We use the cin object alongside the >> operator for taking input.


Example 3: Integer Input/Output

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter an integer: ";
    cin >> num;   // Taking input
    cout << "The number is: " << num;
    return 0;
}

Output

Enter an integer: 70
The number is: 70

In the program, we used

cin >> num;

to take input from the user. The input is stored in the variable num. 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.


C++ Taking 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 << "Number: " << num;

    return 0;
}

Output

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

Please feel free to give your comment if you face any difficulty here.

salman khan

Written by worldofitech

Leave a Reply

How to Fix Wi-Fi Authentication Error on Android

How to Fix Wi-Fi Authentication Error on Android

How to Stop Wi-Fi from Turning on Automatically on Android

How to Stop Wi-Fi from Turning on Automatically on Android