Home >Backend Development >C++ >How to use cin in c++
cin is a stream object in C used to read data from standard input. Usage: 1. Include the header file #include
; 2. Declare the cin object std::cin; 3. Use the >> operator to read the input; 4. Press the Enter key to submit the input, and the input will be stored in in the specified variable.
Using cin in C
What is Cin?
cin is a stream object in the C standard library used to read data from standard input (usually the keyboard).
Usage:
1. Include the header file
<code class="cpp">#include <iostream></code>
2. Declare the cin object
<code class="cpp">std::cin; // 声明 cin 对象</code>
3. Use the >> operator to read the input
<code class="cpp">int num; std::cin >> num;</code>
4. Press Enter to submit the input
After pressing the Enter key, the input will be read from the standard input and stored in the specified variable.
For example:
<code class="cpp">int main() { int num; std::cout << "Enter a number: "; std::cin >> num; std::cout << "The number is: " << num << std::endl; return 0; }</code>
Note:
std::cin.ignore()
function. The above is the detailed content of How to use cin in c++. For more information, please follow other related articles on the PHP Chinese website!