C++整行读入字符串的几种基本方法

版权声明:SupremeBeast3_ https://blog.csdn.net/weixin_43359312/article/details/89133374

C++整行读入字符串的几种基本方法

代码

#include <iostream>
#include <string>
using namespace std;
int main() {
	char str1[6];
	cin.getline(str1, 5);	//实际读取4个 最后一个为'\0'
	//cin.get(str1, 5).get();  //另一种写法 后面的get吃掉换行符
	cout << str1 << endl;
	char str2[10];
	cin.getline(str2, 8, '*');	//实际读取7个 中途读到'*'停止
	cout << str2 << endl;
	//string A;
	//getline(cin, A);	//读入string
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43359312/article/details/89133374