Lesson 3
Input and Output
Most useful programs need to accept input from the user and display output clearly. In C++, this is commonly done with cin and cout.
Example
Example
#include <iostream>
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;
}Try it yourself:
Note: cin >> reads one word at a time. For full names with spaces, you will later use getline().
Book Tip:
This short lesson is part of a larger learning path. The full book expands each topic with more explanation, exercises, and projects.