User Input
cin reads a value typed by the user with >>. getline reads a whole line including spaces. Interactive input cannot run on this page, so the example is a sample session.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "Name: ";
getline(cin, name);
cout << "Age: ";
cin >> age;
cout << name << " will be " << age + 1 << " next year" << endl;
}
// sample run:
// Name: Ada
// Age: 30
// Ada will be 31 next yearExercise
Read a value into x.
cin x;
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.