C provides a variety of input statements: cin statement: reads from standard input and stores it in a variable cin.get() function: reads and returns a character cin.getline() function: reads a line of text And stored in a string variable
C language provides a variety of input methods, the most commonly used iscin
Statement:
cin
statement reads input from the standard input stream and stores it in the specified variable. Its basic syntax is as follows:
<code class="cpp">cin >> variable_name;</code>
where variable_name
is a variable name used to store values read from the standard input stream.
Example: Read an integer and store it in the num
variable:
<code class="cpp">int num; cin >> num;</code>
cin.get()
The function reads a character from the standard input stream and returns that character. Its basic syntax is as follows:
<code class="cpp">char ch = cin.get();</code>
where ch
is a character variable used to store characters read from the standard input stream.
Example: Read a character and store it in the ch
variable:
<code class="cpp">char ch; cin.get(ch);</code>
cin.getline()
The function reads a line of text from the standard input stream (until a newline character is encountered) and stores it in the specified string variable. Its basic syntax is as follows:
<code class="cpp">string line; cin.getline(line, max_length);</code>
Where:
line
is a string variable used to store text read from the standard input stream. max_length
is an integer specifying the maximum length of a line of text to be read. Example: Read a line of text and store it in the line
variable:
<code class="cpp">string line; cin.getline(line, 100); // 最多读取 100 个字符</code>
The above is the detailed content of How to write input statement in c++. For more information, please follow other related articles on the PHP Chinese website!