In C++, you can take user input using the cin object, which is part of the iostream library.
Example: how to take input from the user
#include <iostream>
using namespace std;
int main() {
string name;
// Asking for the user's name
cout << "Enter your name: ";
cin >> name; // Taking input for the name
// Displaying the collected information
cout << "Hello, " << name << endl;
return 0;
}
Explanation:
- cin is used to take input from the user.
- cout is used to output information to the screen.
Output:
Enter your name: John
Hello, John
Hello, John
Example: enter your age
#include <iostream>
using namespace std;
int main() {
int age;
// Asking for the user's age
cout << "Enter your age: ";
cin >> age; // Taking input for the age
// Displaying the collected information
cout << "Your age is " << age << endl;
return 0;
}
Output:
Enter your age: 35
Your age is 35
Your age is 35