Skip to content Skip to sidebar Skip to footer

Input-Output in C++

In this blog, we will learn to use the standard cout object to display output to the user and the standard cin object to take input from the user, with the help of examples. Let’s start with the header files to gain more understanding of cout and cin.

Header files

C++ header files are the standard interface for various languages such as C and C++. They can be described as a protocol for how to implement your programs. In C++, there are different ways to take input and display output to the user; one of the ways is cin and cout with the help of iostream.

Syntax

#include<header_file_name>

Example

#include<iostream>

Standard Output stream(cout)

Cout is the standard output stream in c++, which helps us to display the output to the user. You can also say the printf() of c++.

Syntax

cout<< //text in double inverted comma or variable without inverted comma;

Example

#include <iostream> //Adding header file
using namespace std;

int main()
{
    cout << "I love her, But she loves someone else"; // Dokha
    return 0;
}

OUTPUT

I love her, But she loves someone else

We can also display variables using cout. Here is an example.

#include <iostream> //Adding header file
using namespace std;

int main()
{
    string cheater = "suraj";
    // not you suraj (if any), he was truly a cheater

    cout << "I love her, But she loves " << cheater << endl;
    cout << "Now just have to crack IAS and raid " << cheater << "'s house";
    return 0;
}

OUTPUT

I love her, But she loves suraj
Now just have to crack IAS and raid suraj's house

In the above example, we can add variables with cout along with “<< “in front of the variable or back or both sides as per the requirement.

Standard Input stream(cin)

Read input from the user in C++ is an effortless and promising way to get the input from a user and put them on screen. Cin in c++ is the most straightforward way to get data from the keyboard.

Syntax

cin>> // variable name;

Example

#include <iostream> //Adding header file
using namespace std;

int main()
{
    int contribution;
    cout << "What was your contribution in last boys night? " << endl;
    cin >> contribution;
    // taking input and storing in contribution variable
    cout << "Your contribution in the party was " << contribution;
    // displaying output/contribution of the poor boy

    return 0;
}

OUTPUT

What was your contribution in last boys night? 
500          <--INPUT BY THE USER
Your contribution in the party was 500

In the above example, We are taking input from the user is easy with the help of cin and along with”>>” and later on displaying the output with the help of cout.

Multiple inputs/COmbined Example

#include <iostream>
using namespace std;

int main()
{
    string skill, area_51;
    cout << "Complete the sentence" << endl;
    cout << "Now I learned _____, so now I can hack _____" << endl;

    cin >> skill >> area_51;

    cout << "Now I learned " << skill << ", so now I can hack " << area_51;

    return 0;
}

OUTPUT

Complete the sentence
Now I learned _____, so now I can hack _____
HTML NASA    <----INPUTS  BY THE USER
Now I learned HTML, so now I can hack NASA

In the above example, we are taking input from the user with cin and >>, adding multiple variables to take input, and displaying the output with the cout and <<.

FOLLOW-UP

So, now we can display the output in our program and take the input from the user, and you get all the information about it. Suppose you understood and comprehended this blog properly. Well done. You’re getting advanced daily. If, in any case, you’re finding it difficult, you can let us know in the comments & contact us. Besides, if you’ve any doubts, you can post them in the comments. Don’t worry; our geeks will surely get your doubts cleared soon.

Similarly, you must also know that GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more comprehensive and easy-to-grasp courses for you (for free). Follow us on FacebookInstagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates