1. gets()
C++中保留了C语言的方法
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
char s[100];
gets(s);
cout << s;
return 0;
}
输入:Hello world!
输出:Hello world!
2. cin.getline()
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char s[100];
cin.getline(s,105);//需要注意getline会把换行符取出,不会影响下一次输入
cout << strlen(s);
return 0;
}
输入:Hello world!
输出:12 //strlen这里表示的是输入字符串所占字符数
3. cin.get()
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char a[100];
cin.get(a,100);//注意cin.get()不会把换行符取出,会影响下一次输入
cout << strlen(a);
return 0;
}
输入:Hello world!
输出:12
4. getline()
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string s;
getline(cin,s);
cout << s << endl;
return 0;
}
输入:I like you!
输出:I like you!
写在最后:如果还有其他方法,欢迎大家进行讨论!