Home > Backend Development > C++ > How to enter a character array in c++

How to enter a character array in c++

下次还敢
Release: 2024-05-09 01:09:17
Original
797 people have browsed it

How to enter a character array in C

There are many ways to enter a character array in C:

cin.getline()

  • Syntax: cin.getline(array_name, size, delimiter);
  • Parameters:

    • array_name: The name of the character array
    • size: The size of the character array
    • delimiter : Enter the terminating character (such as newline character)

For example:

<code class="cpp">char str[100];
cin.getline(str, 100, '\n');</code>
Copy after login

cin.get( )

  • Syntax: cin.get(array_name[index]);
  • Parameters :

    • array_name[index]: The index position of the character array

For example:

<code class="cpp">char str[100];
for (int i = 0; i < 100; i++) {
  cin.get(str[i]);
  if (str[i] == '\n') break;
}</code>
Copy after login

gets()

  • Syntax: gets(array_name);
  • Parameters:

    • array_name: The name of the character array

Note: gets() function is unsafe because if the input is too long, it will cause buffer overflow.

fgets()

  • Syntax: fgets(array_name, size, stream);
  • Parameters:

    • array_name: The name of the character array
    • size: Characters The size of the array
    • stream: Input stream

##For example:

<code class="cpp">char str[100];
fgets(str, 100, stdin);</code>
Copy after login

The above is the detailed content of How to enter a character array 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