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:
#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
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:
#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
Enter number1 = 5 Enter number2 = 10 Enter number3 = 15 Multiplication Result = 750
No Comments