User Input

std::cin, which stands for “character input”, reads user input from the keyboard.

Syntax

std::cin >> variable;

Here, the user can enter a value in the terminal, press enter, and that number will get stored in the variable.

Example

In this example, the program will prompt the user to enter a number with "Enter amount: ". Then the user can enter a number, press enter, and that number will get stored in the variable tip.

#include <iostream>
int main() {
int tip = 0;
std::cout << "Enter amount: ";
std::cin >> tip;
std::cout << "You gave a tip of $" << tip << "\n";
}

User Input

getline()
Converts user input into a string delimited by a character, if specified, and then stores it into a variable.
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn C++ on Codecademy

Edit this page on GitHub

Contributors