In this C++ tutorial, you will learn how to read user input with std::cin, how the extraction operator >> works, how to read multiple values, and when to use std::getline() or cin.ignore().
What is cin in C++?
std::cin is the standard input stream object in C++. It is declared in the <iostream> header and normally reads data entered through the program’s standard input, such as the keyboard in an interactive terminal.
The extraction operator >> takes formatted input from cin and stores the converted value in a variable. When a program uses using namespace std;, you can write cin instead of std::cin.
C++ cin syntax with the extraction operator
std::cin >> variable;
For example, if age is an int, the statement std::cin >> age; waits for input, converts a valid integer token, and stores the result in age.
Read an integer with C++ cin
In the following example, we shall read integer from user using cin object.
C++ Program
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a number : ";
cin >> x;
cout << "You entered a number : " << x << endl;
}
Output
Enter a number : 63
You entered a number : 63
Here, cin >> x; reads an integer token and stores it in x. Leading whitespace is skipped automatically for formatted numeric input.
Read multiple values with one cin statement
You can chain the extraction operator to read several values in sequence. The input values are assigned from left to right.
#include <iostream>
using namespace std;
int main() {
int age;
double height;
cout << "Enter age and height: ";
cin >> age >> height;
cout << "Age: " << age << '\n';
cout << "Height: " << height << '\n';
return 0;
}
Enter age and height: 24 1.72
Age: 24
Height: 1.72
The two values may be separated by spaces, tabs, or line breaks because formatted extraction treats whitespace as a separator for these types.
Read a string with C++ cin
In the following example, we shall read a string from user using cin object. cin reads string until the occurrence of a space character.
C++ Program
#include <iostream>
using namespace std;
int main() {
string name;
cout << "Enter a string : ";
cin >> name;
cout << "You entered a string : " << name << endl;
}
Output
Enter a string : tutorialkart
You entered a string : tutorialkart
For a std::string, cin >> name reads one whitespace-delimited word. If the user enters Ravi Kumar, only Ravi is extracted into name. In portable C++ code, include the <string> header when using std::string.
Read a full line with spaces using getline()
Use std::getline() when the input should include spaces.
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
cout << "Enter your full name: ";
getline(cin, fullName);
cout << "You entered: " << fullName << '\n';
return 0;
}
Enter your full name: Ravi Kumar
You entered: Ravi Kumar
Use cin.ignore() before getline() when needed
A common issue appears when operator>> is followed by getline(). Formatted extraction usually leaves the trailing newline in the input buffer. A following getline() can then consume that newline immediately and return an empty string.
One reliable approach is to discard the rest of the current input line before calling getline().
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main() {
int age;
string fullName;
cout << "Enter age: ";
cin >> age;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter full name: ";
getline(cin, fullName);
cout << fullName << " is " << age << " years old.\n";
return 0;
}
cin.ignore(..., '\n') discards characters through the next newline, so the subsequent getline() starts with the next line of user input.
Read a double value with C++ cin
In the following example, we shall read a double from user using cin object.
C++ Program
#include <iostream>
using namespace std;
int main() {
double d;
cout << "Enter a double value : ";
cin >> d;
cout << "You entered : " << d << endl;
}
Output
Enter a double value : 1.32654663
You entered : 1.32655
The displayed value may contain fewer digits because cout uses its default floating-point formatting. This does not mean cin intentionally rounded the value while reading it.
Read a character with C++ cin
In the following example, we shall read char from user using cin object.
C++ Program
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character : ";
cin >> ch;
cout << "You entered : " << ch << endl;
}
Output
Enter a character : m
You entered : m
Formatted extraction into a char skips leading whitespace by default and then reads the next character.
Read a boolean value with C++ cin
When cin extracts into a bool without boolalpha, numeric input is expected. 0 represents false, while 1 represents true. Other numeric values set the stream’s failure state during formatted boolean extraction.
The original program block below is retained unchanged. It declares an int, so it demonstrates integer input rather than boolean extraction.
C++ Program
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a number : ";
cin >> x;
cout << "You entered a number : " << x << endl;
}
Output
PS D:\workspace\cpp> .\main.exe
Enter a boolean value : 1
You entered : 1
PS D:\workspace\cpp> .\main.exe
Enter a boolean value : 4
You entered : 1
PS D:\workspace\cpp> .\main.exe
Enter a boolean value : -9
You entered : 1
PS D:\workspace\cpp> .\main.exe
Enter a boolean value : 0
You entered : 0
PS D:\workspace\cpp> .\main.exe
Enter a boolean value : -25
You entered : 1
The output block above is also retained unchanged from the original article. For actual boolean extraction, declare a bool variable and check whether the extraction succeeds, as shown next.
#include <iostream>
using namespace std;
int main() {
bool value;
cout << "Enter 0 or 1: ";
if (cin >> value) {
cout << "You entered: " << value << '\n';
} else {
cout << "Invalid boolean input\n";
}
return 0;
}
Enter 0 or 1: 1
You entered: 1
Read true or false with boolalpha
Use std::boolalpha when you want the words true and false instead of numeric boolean input.
#include <iostream>
using namespace std;
int main() {
bool enabled;
cout << "Enter true or false: ";
cin >> boolalpha >> enabled;
cout << boolalpha << "You entered: " << enabled << '\n';
return 0;
}
Enter true or false: true
You entered: true
Check cin when the user enters invalid input
If the input cannot be converted to the requested type, cin enters a failure state and the extraction is not successful. Testing the stream before using the result prevents the program from treating invalid input as valid data.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
if (cin >> number) {
cout << "Number: " << number << '\n';
} else {
cout << "Invalid integer input\n";
}
return 0;
}
Enter an integer: hello
Invalid integer input
In longer interactive programs, you may also need cin.clear() to reset the failure state and cin.ignore() to discard unwanted characters before attempting another read.
C++ cin and cout together
cin and cout are commonly used together in console programs. cout writes to standard output, while cin reads from standard input. A prompt can therefore be displayed with cout immediately before the program waits for a value with cin.
#include <iostream>
using namespace std;
int main() {
int quantity;
cout << "Enter quantity: ";
cin >> quantity;
cout << "Quantity entered: " << quantity << '\n';
return 0;
}
Key points about C++ cin
std::cinis the standard input stream object declared in<iostream>.- Use
>>for formatted input such as integers, floating-point values, characters, and whitespace-delimited strings. - Chain
>>operators to read multiple values in one statement. - Use
std::getline()when a string may contain spaces. - When mixing
>>andgetline(), handle the leftover newline, commonly withcin.ignore(). - Check the state of
cinwhen user input may not match the expected data type.
In this C++ Tutorial, we learned how to use cin object to read input from user.
TutorialKart.com