在C++中,輸入字符串的方法有以下幾種:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
cout << "You entered: " << str << endl;
return 0;
}
#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;
}
#include <cstdio>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}
#include <cstdio>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
printf("You entered: %s\n", str);
return 0;
}