21 Jan C++ User Input
The cin is used in C++ for User Input. It is used to read the data input by the user on the console. Let us see an example C++ program to get input from the user.
C++ program to print a string entered by the user
Let us see an example to print a string entered by the user:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main() { // Declaring Variables string str; # Asks the user to enter a string cout << "Enter a string = "; cin >> str; # Displays the string entered by the user above cout <<"\nThe string entered by user = "<< str; return 0; } |
Output
1 2 3 4 |
Enter a string = The string entered by user = amit |
C++ program to print numbers entered by the user
Let us see an example to multiply 3 numbers entered by the user:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> using namespace std; int main() { // Declaring Variables int a, b, c, res; cout << "Enter number1 = "; cin >> a; cout << "Enter number2 = "; cin >> b; cout << "Enter number3 = "; cin >> c; // Calculate Multiplication res = a * b * c; cout <<"\nMultiplication Result = "<< res; return 0; } |
Output
1 2 3 4 5 6 |
Enter number1 = 5 Enter number2 = 10 Enter number3 = 15 Multiplication Result = 750 |
No Comments