Cin.getline终极用法:
Cin.getline(输入的字符数组, 字符最大个数, 终止字符);
这个终止字符非常好用,就是定义一个字符,输入完你要的字符串,最后加上一个终止字符,再按下回车,就会终止输入,这一点在特定程序中可以达到很好的效果。
当然,cin.getline的括号中也可以不要终止字符,不要终止字符的结果就是输入一行字符串,按下回车就代表输入完成,而带有终止字符的字符串可以换行输入。
实例:
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main() {
char str[101];
cin.getline(str, 1000);
cout << str << endl;
return 0;
}
↑没有终止字符
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main() {
char str[101];
cin.getline(str, 1000, '/');
cout << str << endl;
return 0;
}
↑有终止字符