C++ Program To Take Input From User

In this C++ program, we will learn about taking input from user using cin stream.

For Input, C++ programming language uses an abstraction called streams which are sequences of bytes. cin is a predefined object of class istream. cin object by default is attached to the standard input device which is keyboard in most programming environments. cin along with extraction operator (>>) is used to take keyboard input from user.
Extraction operator is followed by a variable where the input data is stored. cin is an input statement, hence programs waits for user to enter input from keyboard and press enter. Input data flows directly from keyboard to variable.

For Example :
Taking one integer input from user.
int count;
cin >> count;
Taking multiple integer input from user.
int count, sum;
cin >> count >> sum;

The cin can be used to receive the input data like integer, character, float, double etc. Based on the data type of the variable after extraction operator(>>) cin determine how it interprets the characters read from the input.

C++ Program to Take Input From User Using Cin

// C++ Program to take input from user
#include <iostream>
using namespace std;

int main() {
    char word[40];
    int i_var;
    float f_var;
    // Taking integer input from user 
    cout<<"Enter an integer\n";
    cin >> i_var;
    // Taking float input from user
    cout << "Enter a floating point value\n";
    cin >> f_var;
    // Taking a word as input from user
    cout << "Enter a word\n";
    cin >> word;
    // Printing values 
    cout << i_var << endl << f_var << endl 
         << word;
    return 0;
Enter an integer
Enter a floating point value
Enter a word

In above program, we take an integer, a float and a string as input from user and store it in variable i_var, f_var and word respectively using cin. Then we print the values entered by user using cout.

Recommended Posts
C++ Program to Read an Integer and Character and Print on Screen
C++ Program for Input and Output of Integer
C++ Program to Print Hello World
C++ Program to Check Whether a Character is Vowel or Not
C++ Program to Multiply two Numbers
C++ Program to Add Two Numbers
C++ Program to Find Sum of Natural Numbers
C++ Program to Print a Sentence on Screen
C++ Program to Check Whether Number is Even or Odd
C++ Program to Find LCM and GCD of Two Numbers
C++ Program to Check Whether a Character is Vowel or Consonant
All C++ Programs