codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ user input

C++ user input

Read with cin.

cin >> x reads one value. Use getline(cin, s) to read a whole line with spaces.

Example code

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n; string name;
  cout << "Number: "; cin >> n;
  cin.ignore();
  cout << "Name: "; getline(cin, name);
  cout << n << " " << name << endl;
  return 0;
}

Run this code in the online editor

← C++ variablesC++ data types →