The cin
object (pronounced "see-in"), together with the >>
operator, is used to get user input.
cin
is a predefined system variable that reads data from the keyboard.
To use the cin
object, you must include the iostream
class into your program by adding the
statement to the beginning of your program.
The following is an example of how to use the cin
object:
// Create an integer variable named x
int x;
// Instruct the user to type a number with their keyboard and press enter
cout << "Type a number and then press Enter: " ;
// Listen for and get the value entered, and then assign that value to the variable x
cin >> x;
// Display the users inputed value back to them
cout << "Your number is: " << x;
The following is another example of prompting the user for their name and age, as shown in a complete program:
#include
using namespace std;
int main() {
// declare variables
int age;
string name;
// ask the user to enter their name
cout << "Enter your name: " ;
cin >> name;
// ask the user to enter their age
cout << "Enter your age: " ;
cin >> age;
// output the results
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
// exit main
return 0;
}
Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.
About | Contact Us | Privacy | Terms & Conditions | © 2025 - T&J Divisions, LLC, All Rights Reserved |