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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours