C++ Standard Input

Introduction

C++ provides facilities for accepting input from a user.

We can think of the standard input as our keyboard.

A simple example accepting one integer number and printing it out is:

#include <iostream> 

int main() //from  ww  w . ja  v  a2  s .c om
{ 
    std::cout << "Please enter a number and press enter: "; 
    int x = 0; 
    std::cin >> x; 
    std::cout << "You entered: " << x; 
} 

The std::cin is the standard input stream.

It uses the >> operator to extract what has been read into our variable.

The std::cin >> x; statement means: read from a standard input into a x variable.

The cin object resides inside the std namespace.

So, std::cout << is used for outputting data and std::cin >> is used for inputting the data.




PreviousNext

Related