Home  >  Article  >  Backend Development  >  Function of cin.get(ch) function in c++

Function of cin.get(ch) function in c++

下次还敢
下次还敢Original
2024-04-28 19:03:13663browse

The function of the cin.get(ch) function is to read a single character from the standard input and store it in the character variable ch. 1. Read a character from standard input and store it in ch. 2. Return the ASCII code of the character read. 3. If the input stream reaches the end of file, return EOF.

Function of cin.get(ch) function in c++

cin.get(ch) function function

cin.get(ch) is in the C standard library Member function of the input stream object (cin) that reads a single character from standard input and stores it in the specified character variable ch.

How it works

When cin.get(ch) is called, it will:

  • Read a character from standard input .
  • Store the read characters in the character variable ch.
  • Returns the ASCII code of the character read.

If the input stream has reached the end of the file, cin.get(ch) returns EOF (end of file flag).

Usage

cin.get(ch) is often used to read a single character from the standard input, for example:

<code class="c++">char ch;
cin.get(ch);</code>

The above code reads from the standard input Reads a character from and stores it in the ch variable.

Note

  • The cin.get(ch) function cannot be used to read space characters.
  • If there are space characters in the input stream, you need to use the cin.ignore() function to ignore them.
  • cin.get(ch) function reads characters, not strings. To read a string, you need to use the cin.getline() function.

The above is the detailed content of Function of cin.get(ch) function 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