Example 1:
The first code is here where we get an integer value from the user. In the first line, we include the “iostream” header file because we need to get the input and display the output here. The declaration of the “cin” and “cout” functions is done in this header file. Then, the “std” namespace is added since it defines the functions like “cin” and “cout”. Therefore, if we insert the “namespace std” at the beginning of our code, we don’t have to type “std” with each function.
Then, we call the “main()” function and subsequently declare a variable of integer data type. The “int_value” is the variable’s name that we declared here. After this, we utilize the “cout” which aids in printing the given data on the screen. Here, we display a message to the user in which we say, “Enter an integer number”. Below this, we place “cin” along with the extraction symbols “>>” and place the “int_num” variable.
Now, we take the user’s input and save it here. We also want to display the integer number that the user enters here on the screen. So, we utilize the “cout” below this and place “int_value” there.
Code 1:
using namespace std;
int main() {
int int_value;
cout <> int_value;
cout << "The integer number is: " << int_value;
return 0;
}
Output:
When the given code is executed, we enter “89” and it takes “89” as the user’s input. Then, we hit “Enter”, so it displays the next line.
Example 2:
We need to obtain the input and display the output here. Hence, we include the “iostream” header file in the first line. This header file contains declarations for the “cin” and “cout” functions. Next, the “std” namespace is added. We don’t need to type “std” with each function if we add “namespace std” at the start of our code.
Following the call to the “main()” function, a “float” data type variable is declared. The variable that we declare here is called “float_value.” Next, we use the “cout” function to help render the provided data on the terminal. Here, we tell the user to enter a float number by displaying a message. The “float_num” variable and the extraction symbols “>>” are positioned below this along with “cin.”
The user’s input is gathered and saved in the “float_num”. We use the “cout” function once more beneath this and insert the “float_value” since we also want to see the float number that the user enters on the screen.
Code 2:
using namespace std;
int main() {
float float_value;
cout <> float_value;
cout << "The float number is: " << float_value;
return 0;
}
Output:
When the previously mentioned code runs, we type “87.5” from the keyboard as the user’s input. The next line displays the float value when we click “Enter”.
Example 3:
Now, let’s get the “double” data type value from the user. Here, we initialize the “double_value” of the “double” data type and then place the message that we want to show the user. After this, we utilize the “cin>>” and place the “double_value” variable here. The user’s input is saved in this “double_value” variable. We utilize the “cout” again where we inserte the “double_value” variable to display the input that the user enters as the output.
Code 3:
using namespace std;
int main() {
double double_value;
cout <> double_value;
cout << "The double number is: " << double_value;
return 0;
}
Output:
Here, we enter the double data type number and hit “Enter”. But here, we note that it doesn’t display the complete number. To display the complete “double” data type number, we just need some techniques in C++.
Example 4:
Here, we include one more header file which is “bits/stdc++.h” as it contains all the desired function declarations. Here, we set the desired message for the user to see after initializing the “double_d” of the “double” data type. Next, we use the “cin>>” function and assign the “double_d” variable here. The input is saved in the “double_d”. To display the input that the user enters here as the output, we enter the “double_d” variable again into the “cout” field. We also include the “setprecision()” function in which we add “10” so it adjusts the precision of the double data type value and print it accordingly. The precision that we set here is “10”.
Code 4:
#include
using namespace std;
int main() {
double d_value2;
cout <> d_value2;
cout << setprecision(10) << "The double number is: " << d_value2;
return 0;
}
Output:
Here, it gathers the input and then display the “double” value according to the same precision that we adjusted in the given code.
Example 5:
In this code, we get the character’s input from the user. We initialize a “char” variable “char1” here and then utilize the “cout” to display the message. Then, we place “cin>>” and put this “char1” there. So, the character that is entered by the user is stored here. Then, we utilize the “cout” again to show the character that is stored in the “char1” variable.
Code 5:
using namespace std;
int main() {
char char1;
cout <> char1;
cout << "The character is: " << char1;
return 0;
}
Output:
After execution, this message is displayed. We type “z” as the character input. Then, in the next line, the entered character is displayed.
Example 6:
We receive the string input from the user in this code. Here, we initialize the “string” variable “myName” and use “cout” to output the message. Next, we insert “myName” and “cin>>” at that location. Thus, the string that the user entered is saved here. Next, we use the “cout” command again to display the string that is saved in the “myName” variable.
Code 6:
using namespace std;
int main() {
string myName;
cout <> myName;
cout << "My name is: " << myName;
return 0;
}
Output:
The following message appears. We enter the “Peter” string in the input field. The entered string is then shown in the following line:
Example 7:
When we want to take multiple strings or a line as the input, we must use the “getline()” function. We declare the “string Name” here. Then, the message that we enter is printed using “cout”. We then put “Name” and “cin” in the “getline()” function which gets multiple string inputs from the user and store them in the “Name” variable. This saves the strings that the user entered. Next, we display the strings that are saved in the “Name” variable using the “cout” command.
Code 7:
using namespace std;
int main() {
string Name;
cout << "Please Enter your full name here: ";
getline (cin, Name);
cout << "Your full name is: " << Name;
return 0;
}
Output:
Here, in this outcome, we enter “James Samuel” as the string data. When we hit “Enter”, it displays the complete name here since we utilized the “getline()” function.
Conclusion
We discussed about the “C++ user input” in this guide. We explored this concept and learned that the “cin” command, along with the extraction symbols “>>”, is utilized to get the user’s input. We took the input of the integer, float, double, char, and string data type values from the user with the help of the “cin>>” command and demonstrated the C++ examples in which the “user input” concept is explained in detail.