User Input

Published Jun 4, 2021Updated Dec 21, 2022
Contribute to Docs

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.

All contributors

Looking to contribute?

Learn C++ on Codecademy