C++ cin.getline()函数的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w442863748/article/details/50679060

cin.getline()函数作用:接收一个字符串,可以接收空格

头文件:#include <iostream>

使用例:

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <vector>
#include <sstream>
#include <list>
#include <algorithm>
#include <time.h>
#include <stdarg.h>

#include "myLibrary.h"
//头文件引用的较多,有一些和本程序无关

using namespace std;

int main(int argc, char *argv[])
{
	char cinstr[10];//输入ab cdefg hijklmnopq
	cin.getline(cinstr, 5);//支持空格,以'\0'结束,所以只能看到4个字符
	cout << cinstr << endl;
	cin.clear();//必须有,否则第二次getline得不到数据
	cin.getline(cinstr, 10, 'i');//支持3个参数,第三个参数为结束字符
	cout << cinstr << endl;
	system("pause");
	return 0;
}
执行结果:



猜你喜欢

转载自blog.csdn.net/w442863748/article/details/50679060