Skip to content
Justice Tech School

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 year

Exercise

Read a value into x.

cin  x;

Quiz Test

Which function reads a full line?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.