C++ User Input (cin)
User input means data typed by the person running a program. In C++, std::cin reads input from the keyboard and stores it in variables.
Input lets a program respond to the user instead of always using fixed values. You usually print a short prompt with std::cout, then read the answer with std::cin.
Using std::cin
To use std::cin, include the <iostream> header. The extraction operator, written as >>, takes input from std::cin and puts it into a variable.
#include <iostream>
int main(void) {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old." << std::endl;
return 0;
}
Output:
Enter your age: You are 18 years old.
This sample output assumes the user typed 18 and pressed Enter.
How It Works
The statement int age; creates a variable that can store one whole number. The prompt tells the user what kind of value to type.
When the program reaches std::cin >> age;, it waits until the user enters input. If the user types 18, that value is stored in age. The final output statement prints the value that was saved.
The type of the variable matters. If age is an int, the input should be a whole number. For decimal values, use a type such as double. For text, use std::string.
Reading More Than One Value
You can read multiple values by chaining more than one >> operation. Whitespace, such as spaces, tabs, and newlines, separates the values.
#include <iostream>
int main(void) {
int first;
int second;
std::cout << "Enter two numbers: ";
std::cin >> first >> second;
std::cout << "Sum: " << first + second << std::endl;
return 0;
}
Output:
Enter two numbers: Sum: 13
This sample output assumes the user typed 8 5 and pressed Enter. The user could also type 8, press Enter, then type 5 and press Enter again.
Reading Text
To store text in a C++ string, include <string> and create a std::string variable. With std::cin >>, C++ reads one word at a time and stops at whitespace.
#include <iostream>
#include <string>
int main(void) {
std::string name;
std::cout << "Enter your first name: ";
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
Output:
Enter your first name: Hello, Ada!
This sample output assumes the user typed Ada and pressed Enter.
Reading A Full Line
If you want to read text that may contain spaces, use std::getline. It reads characters until the user presses Enter, so it can store a full name or sentence.
#include <iostream>
#include <string>
int main(void) {
std::string fullName;
std::cout << "Enter your full name: ";
std::getline(std::cin, fullName);
std::cout << "Welcome, " << fullName << "." << std::endl;
return 0;
}
Output:
Enter your full name: Welcome, Ada Lovelace.
This sample output assumes the user typed Ada Lovelace and pressed Enter.
Common Beginner Mistakes
- Forgetting
#include <iostream>before usingstd::cinorstd::cout. - Using
<<withstd::cin. Input uses>>; output uses<<. - Trying to read a sentence with
std::cin >> text. Usestd::getlinefor full lines. - Typing input that does not match the variable type, such as letters when the program expects an
int. - Forgetting that the program waits at
std::cinuntil the user enters a value.
Takeaway: use std::cin with >> to read typed values into variables, and use std::getline when you need a whole line of text.
