User Input
Program to take input from user and display it
BeginnerTopic: Basic Programs
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;
}Output
Enter your name: Alice Enter your age: 25 Hello, Alice! You are 25 years old.
Understanding User Input
This program demonstrates user input using cin. The cin operator reads input from the keyboard. We can read different data types (string, int) using cin. The program prompts the user, reads their input, and displays it back.
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.