Std::cin

From aldeid
Jump to: navigation, search

Syntax

extern istream cin;

Description

Standard input stream

Object of class istream that represents the standard input stream oriented to narrow characters (of type char). It corresponds to the C stream stdin.

The standard input stream is a source of characters determined by the environment. It is generally assumed to be input from an external source, such as the keyboard or a file.

As an object of class istream, characters can be retrieved either as formatted data using the extraction operator (operator>>) or as unformatted data, using member functions such as read.

The object is declared in header <iostream> with external linkage and static duration: it lasts the entire duration of the program.

Example

Source Run
#include <iostream>
#include <string>

using namespace std;

int main()
{
  int age(0), nkids(0);
  string name("");

  cout << "What's your name? ";
  getline(cin, name);

  cout << "How old are you? ";
  cin >> age;

  cout << "How may kids do you have? ";
  cin >> nkids;

  cout << "Hello " << name << ". You are " << age << " years old and have " << nkids << " kids." << endl;
  return 0;
}
$ ./main
What's your name? Alice Shaw
How old are you? 38
How may kids do you have? 3
Hello Alice Shaw. You are 38 years old and have 3 kids.