User Input

Beginner-friendly C++ program that teaches how to take input from the user using cin and display it on the screen.

BeginnerTopic: Basic Programs
Back

C++ User Input Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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.

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:

<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.

---

                  #include <iostream>  
                  #include <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:

cin >> name;

does two things:

1.cout prints the message "Enter your name:"
2.cin waits for the user to type something from the keyboard
3.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.

---

                  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:

"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.

---

                  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

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.

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.

Table of Contents