Home > Backend Development > C++ > How to write input statement in c++

How to write input statement in c++

下次还敢
Release: 2024-05-08 01:48:17
Original
557 people have browsed it

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

How to write input statement in c++

Input statement in C

C language provides a variety of input methods, the most commonly used iscin Statement:

cin 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>
Copy after login

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>
Copy after login

cin.get() function

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>
Copy after login

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>
Copy after login

cin.getline() function

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>
Copy after login

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>
Copy after login

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!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template