VOOZH about

URL: https://www.geeksforgeeks.org/cpp/input-in-cpp/

⇱ Input in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Input in C++

Last Updated : 2 Jun, 2026

Input in C++ refers to the process of accepting data from the user or an external source so that a program can perform operations on that data. User input makes programs interactive and allows them to work with dynamic values instead of fixed data.

  • Enables interaction between the user and the program.
  • Allows data to be stored in variables at runtime.
  • Can be used to read single values, multiple values, or entire lines of text.

Common Input Methods in C++

C++ provides several ways to accept input from the user. The most commonly used methods are:

Using cin (Standard Input Stream)

The cin object is part of the <iostream> library and is used to read formatted input from the standard input device (usually the keyboard). It uses the extraction operator (>>) to read values and store them in variables.

Input:

10

Output:

10

Explanation:

  • The cin >> i; statement reads an integer value from the user.
  • The value entered is stored in variable i.
  • The output is displayed using cout.

Taking Multiple Inputs Using cin

The cin object can read multiple values in a single statement by chaining the extraction operator (>>).

Input:

ABC
10

Output:

Name : ABC
Age : 10

Explanation:

  • The cin >> name >> age; statement reads a string followed by an integer.
  • Each value is stored in the corresponding variable in the order entered.
  • cin stops reading a string at whitespace, making it suitable for single-word inputs.

Using getline() for Multi-word Input

The getline() function is used to read an entire line of text, including spaces.

Input

John Smith

Output

John Smith

Explanation:

  • getline(cin, name) reads the complete line entered by the user.
  • Unlike cin, it does not stop at spaces.
  • Useful for reading names, addresses, and sentences.

Using cin.get() to Read a Character

The cin.get() function is used to read a single character from the input stream.

Input

A

Output

A

Explanation:

  • cin.get(ch) reads a single character from the input.
  • It can also read whitespace characters such as spaces and newlines.
  • Useful when character-level input is required.
Comment