Which function is used to read a single character from the console in C++ ?
Please first select an option.
The correct answer is C) cin.get(ch)
In C++, the cin object is used to read input from the console (standard input). The cin.get() function is specifically used to read a single character from the console. Let's briefly explain each option:
- A) read(ch) : There is no standard function called read() in C++ to read a single character. This is an invalid option.
- B) scanf(ch) : scanf() is a C function, not a C++ function. It is used to read formatted input, but it requires a format specifier (e.g., %c for a character). So, the correct usage should be scanf("%c", &ch) to read a single character.
- C) cin.get(ch) : This is the correct answer. The cin.get() function is a member function of the cin object. It reads a single character from the console and stores it in the variable ch.
- D) getline(ch) : The getline() function is used to read a line of text from the console, not a single character. It reads until it encounters a newline character (Enter key).