Home > Article > Backend Development > How to enter letters in c++
How to enter letters in C? Input can be done in three ways: using cin and istream_iterator. Use getline. Use a character array.
How to enter letters in C
To enter letters in C, you can use the following methods:
1. Use cin and istream_iterator
<code class="cpp">#include <iostream> #include <iterator> int main() { std::istream_iterator<char> it(std::cin); char c = *it; std::cout << "输入的字母:" << c << "\n"; return 0; }</code>
2. Use getline
<code class="cpp">#include <iostream> int main() { std::string input; std::cout << "输入一个字母:" << std::endl; std::getline(std::cin, input); char c = input[0]; std::cout << "输入的字母:" << c << "\n"; return 0; }</code>
3. Use character array
<code class="cpp">#include <iostream> int main() { char c[2]; std::cout << "输入一个字母:" << std::endl; std::cin.getline(c, 2); std::cout << "输入的字母:" << c << "\n"; return 0; }</code>
Note:
The above is the detailed content of How to enter letters in c++. For more information, please follow other related articles on the PHP Chinese website!