#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
return 0;
}Output
Enter your name: Alice Enter your age: 25 Hello, Alice! You are 25 years old.
User Input in C++
This program teaches you how to take input from the user in C++. You will learn how to read text and numbers using cin and how to show them on the screen using cout. It is one of the most important concepts for beginners because almost all real programs need user input.
1. Header Files
The program starts with:
#include <iostream>
#include <string>
- <iostream> allows us to use cout (for output) and cin (for input).
- <string> allows us to use the string data type to store words or text.
Without these header files, the program would not understand cout, cin, or string.
2. using namespace std;
This line makes it easy to use names like cout, cin, and string without writing std:: again and again.
Without it → std::cout, std::cin
With it → cout, cin
This keeps the program simple for beginners.
3. Declaring Variables
Inside the main function, we have:
string name;
int age;
Here's what this means:
-
string name; creates a variable that can store text.
We will use this to store the user's name. -
int age; creates a variable that can store numbers (without decimals).
We will use this to store the user's age.
Variables are like containers that hold information the user gives.
4. Taking Input From the User
To take input, the program uses cin.
The line:
cout << "Enter your name: ";
cin >> name;
does two things:
- cout prints the message "Enter your name:"
- cin waits for the user to type something from the keyboard
- Whatever the user types gets stored inside the variable name
The same happens for age:
cout << "Enter your age: ";
cin >> age;
The user types a number like 25, and that value is stored inside the variable age.
Important Note About cin and Strings
cin >> name will only read one word.
If the user types "Alice Smith", only "Alice" will be stored.
To read full sentences, we use getline(), which you will learn later.
5. Displaying the Output
This line:
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
prints a message using both text and the values stored in name and age.
Let's break it:
- "Hello, " → fixed text
- name → whatever user typed
- "! You are " → more text
- age → the number user typed
- " years old." → final text
- endl → moves to the next line
So the complete sentence becomes:
Hello, Alice! You are 25 years old.
6. return 0;
This ends the program and tells the computer that everything ran correctly.
Summary
- cin is used to take input from the user.
- cout is used to print output on the screen.
- string variables store text, and int variables store numbers.
- The program asks for name and age and shows them back to the user.
- This is a basic and essential program for learning user interaction in C++.
With this program, beginners understand how to take user input and work with variables, preparing them for more advanced topics like loops, conditions, and full applications.