C Methods for inputting strings: 1) Direct input: cin >> str; 2) Specified size input: cin.get(str, size); 3) Whole line input: getline(cin, str).
How to enter a string in C
Directly enter
getline
The following example demonstrates these methods:
<code class="c++">int main() { string str1, str2; // 直接输入单词 cout << "请输入一个单词:" << endl; cin >> str1; // 使用 getline 输入整行 cout << "请输入一行文字:" << endl; getline(cin, str2); // 输出输入的字符串 cout << "输入的单词:" << str1 << endl; cout << "输入的行:" << str2 << endl; return 0; }</code>
Notes:
The above is the detailed content of How to enter a string in c++. For more information, please follow other related articles on the PHP Chinese website!