User Input
Beginner-friendly C++ program that teaches how to take input from the user using cin and display it on the screen.
C++ User Input Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}Enter your name: Alice
Enter your age: 25
Hello, Alice! You are 25 years old.Understanding User Input
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:
Without these header files, the program would not understand cout, cin, or string.
---
#include <iostream>
#include <string>2. using namespace std;
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:
We will use this to store the user's name.
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:
cin >> name;
does two things:
The same happens for age:
cin >> age;
The user types a number like 25, and that value is stored inside the variable age.
---
cout << "Enter your name: ";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:
prints a message using both text and the values stored in name and age.
Let's break it:
So the complete sentence becomes:
Hello, Alice! You are 25 years old.
---
cout << "Hello, " << name << "! You are " << age << " years old." << endl;6. return 0;
This ends the program and tells the computer that everything ran correctly.
---
Summary
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.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for User Input
This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.