在C++中,输入字符串的方法有以下几种:
- 使用cin:
#include <iostream>#include <string>using namespace std;int main() {string str;cout << "Enter a string: ";cin >> str;cout << "You entered: " << str << endl;return 0;}- 使用getline:
#include <iostream>#include <string>using namespace std;int main() {string str;cout << "Enter a string: ";getline(cin, str);cout << "You entered: " << str << endl;return 0;}- 使用scanf:
#include <cstdio>int main() {char str[100];printf("Enter a string: ");scanf("%s", str);printf("You entered: %s\n", str);return 0;}- 使用fgets:
#include <cstdio>int main() {char str[100];printf("Enter a string: ");fgets(str, 100, stdin);printf("You entered: %s\n", str);return 0;}上一篇:如何用eclipse的jsp编写网页
下一篇:php中ceil函数的用法是什么
C++









