C++

C++ Using std::cin

In C++, we need a mechanism to interact with users, or to get information from the users. For this purpose, C++ provides a standard library to entertain the input-output statement. The ‘iostream’, here ‘io’ means input and output, this stream deals with the input and output statements. This library contains all the methods that we need to input data from the user or output data on the console. First, we import this library and to input data we use ‘cin>>’.

The ‘iostream’ library has predefined variables ‘std::cin’ is one of them. The ‘std’ stands for standard and ‘cin’ means character input. The ‘iostream’ supports many built-in functions that we can use in our code by just importing the library.

Syntax

First Method for Using ci:

std::cin>>variable_name;

The ‘std’ is standard, ‘::’ tells the compiler about the next statement and the operator ‘>>’ is used to input the data that will be stored in the ‘variable_name’. The input type depends on the data type of the given variable. If ‘variable_name’ is of integer type it will accept input of integer type, so the ‘float’ or other types of input cannot be stored in it.  If we try to save different types of data in an integer type, the compiler will show an error message on the console.

To make this statement functional, we need to import the relevant library that is:

#include<iostream>

Second Method for Using cin:

The other method is to first make the ‘standard namespace’. By creating this, we do not need to write the term ‘std’ with every statement that belongs to the standard class.

using namespace std;

This means that we will be using standard objects in our code. After doing this, we will be free from writing ‘std’ every time we use any object of the standard class.

cin>>variable_name;

Now, this statement can successfully be executed without using ‘std’.

Example no. 1:

In this code, we will get data from the user, that data should be of integer type and we will utilize ‘std::cin’ to get data.

#include <iostream>
int main() {
    int number_0;
    std::cout <> number_0;
    std::cout<< "The integer number you entered is: "<< number_0;
    return 0;
}

In the main() method, we have to declare a variable ‘number_0’ having an integer type. Then, we used the ‘std::cout’ statement to print a text on the console. The ‘std’ is utilized before the ‘cout’ because this statement is an output statement from the standard library. The ‘std::’ will be placed before any input/output statement. After this, the ‘std::cin’ is employed to get the integer type data from the user. C++ compiler compiles the code line by line, first, it will execute the ‘std::cout’ statement. Then, it will wait on the screen to take input from the user because of the ‘std::cin’ statement when the user gives input through the keyboard. The compiler will store that value in the specific variable ‘number_0’ and it is placed after the ‘std::cin>>’. The compiler implements the next statement which is of ‘std::cout’. It will represent a message and value that we took from the user.

Example no. 2:

In the example, we explain to you how we can take the data of string type from the user without using ‘std::cin’.

#include <iostream>
using namespace std;
int main() {
    string Name_0;
    cout <> Name_0;
    cout<< "Your Name is   :  "<< Name_0;
    return 0;
}

After importing the ‘iostream’ library, add one line of code ‘using namespace std’. By this, we can save time as we cannot utilize ‘std’ again whenever we use any object of ‘std’. Within the main() function, there is a declaration of a string type variable ‘Name_0’ to store the name of a user. Then, the ‘cout’ statement will be employed to print the message that we want to display on the terminal. One thing you should notice is that we have not used ‘std’ before ‘cout’ because we have globally defined it. The ‘cin>>’ is utilized to take the name from the user and display that name by using ‘cout’. The compiler will show the first ‘cout’ message and then it will wait for the user to enter a string. When the user enters the string and hits the ‘ENTER’ key, the compiler will implement the next statement and it will print the message with the name of a user.

Example no. 3:

The code will demonstrate how to acquire multiple user inputs.

#include <iostream>
int main() {
    float weight_0;
    int roll_no;
    std::cout <>  weight_0;
    std::cout <> roll_no;
    std::cout<< "Your weight is :  "<<  weight_0;
    std::cout<< "\nYour Roll No is:  "<< roll_no;
    return 0;
}

In the program, first, introduce the library. Then, inside the main() method define one float type variable ‘weight_0’ and one integer type variable ‘roll_no’ to get the weight and roll number of the user. Show the message by calling ‘std::cout’. Take the weight from the user by using ‘std::cin’. The compiler will display the text and waits for the user to enter the weight. Then, it executes the next command and waits for the user to enter a roll number. With the help of ‘cout’, we will represent the weight and roll number entered by the user. By doing this, we can get more than one input from the user.

Example no. 4:

This program describes how we can get three inputs from the user by just employing one ‘std::cin’ statement.

#include <iostream>
int main() {
    float  number_1, number_2,number_3;
    std::cout <>  number_1>>number_2>>number_3;
    std::cout<< "Sum of three numbers is :  "<< number_1+number_2+number_3;
    return 0;
}

Incorporate the ‘iostream’ header file. In the next statement, invoke the main() function and define three floating point variables. These variables are separated by commas. Then, we want to print the text on the terminal so utilize the ‘cout’ command. To take the values from the user, employ the ‘std::cin’. Here we use one ‘std::cin’. After mentioning the first variable name ‘number_1’, utilize ‘>>’ and then place the other one ‘number_2’, and so on. The compiler will take three inputs from the user. Then it will add these three values and display the sum of them. The compiler will implement the first ‘cout’ command and print the message. Then, it executes the ‘cin’ command and waits to take the input. When a user enters the first value and presses the space or ‘ENTER’ button, it will take the second input and then again press space or ‘ENTER’ to enter the third value. When we hit ‘ENTER’, the compiler initializes the first variable and when we enter a second value and hit ‘ENTER’ it stores a value in the second variable, and so on.  

Conclusion

In this guide, we have covered the topic ‘std::cin’ which is utilized to input the data from the user and to use this statement we have to import the relevant library. We have also talked about the purpose of this statement in C++ and its requirement. The syntax of this command is also discussed with numerous codes. The examples explain in detail how the ‘std::cin’ statement can be utilized in different ways to take input from users.

About the author

Admin

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.