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

How to enter a character array in c++

下次还敢
下次还敢Original
2024-05-09 01:09:17753browse

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>

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>

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>

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!

Statement:
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
Previous article:The role of class in c++Next article:The role of class in c++