C++之输入方式cin、cin.get()、cin.getline()、getline()


一、cin

接收一个字符串,遇“空格”、“TAB”、“回车”就结束

这点很重要,输入一个带空格的字符串,就只能截留空格之前的部分字符串

/*char字符数组*/
#include <iostream>
using namespace std;
int main()
{
	char a[20];
	cin >> a;
	cout << a << endl;
	/*
	hello world
	hello
	*/
	return 0;
}
/*string类型*/
#include <iostream>
using namespace std;
int main()
{
	string str;
	cin >> str;
	cout << str << endl;
	/*
	hello world
	hello
	*/
	return 0;
}

二、cin.get()

1.得到一个字符

cin.get(ch);ch=cin.get();

#include <iostream>
using namespace std;
int main()
{
	char ch;
	cin.get(ch);
	//ch=cin.get();
	cout << ch << endl;
	/*
	输入:abc
	输出:a
	结束
	*/
	return 0;
}

2.cin.get(数组,size)

只会接收size-1个字符(即可以接受带空格的),因为cin.get()要保留一个字符'\0'用来结尾。

#include <iostream>
using namespace std;
int main()
{
	char a[20];
	cin.get(a,12);
	cout << a << endl;
	/*
	输入:hello world
	输出:hello world
	*/
	return 0;
}

3.cin.get()缓存问题

cin.get(无参数):用于舍弃输入流中的不需要的字符,或者舍弃回车(用来清除缓存)

/*有缓存问题*/
#include <iostream>
using namespace std;
int main()
{
	char ch;
	cin.get(ch);
	cout << ch << endl;

	cin.get(ch);
	cout << ch << endl;
	/*
	输入:a
	输出:a
	(回车)
	(回车)
	结束
	*/
	//这两个回车是因为输入a后残留的回车被第二次cin.get(ch)收到了
	return 0;
}
/*解决缓存问题*/
#include <iostream>
using namespace std;
int main()
{
	char ch;
	cin.get(ch);
	cout << ch << endl;

	cin.get();
	cin.get(ch);
	cout << ch << endl;
	/*
	输入:a
	输出:a
	输入:b
	输出:b
	结束
	*/
	return 0;
}

三、cin.getline()

作用:

接受size个字符串(即可以接受带空格的)

原型:

cin.getline(接收字符串的变量, 接收字符个数 ,判断结束字符='\0')
当第三个参数省略时,系统默认为’\0’

/*以'\0'为判断结束的字符*/
#include <iostream>
using namespace std;
int main()
{
	char a[20];
	cin.getline(a,12);
	cout << a << endl;
	/*
	输入:hello world
	输出:hello world
	*/
	return 0;
}

四、getline()

1.使用string头文件

作用

接受一行字符串到string变量中(即可以接受带空格的)

头文件:

#include <string>
using namespace std;

区别:

cin.getline()getline()不是同一个东西,前者在#include<iostream>中,后者在#include<string>中。

2.原型

getline(输入流, 接收输入的string类型的变量);

其中:输入流可以是控制台输入流cin,也可以是文件输入流ifstream。

PS:没有getline()

/*标准输入流cin*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	getline(cin,str);
	cout << str << endl;
	/*
	输入:hello world
	输出:hello world
	*/
	return 0;
}
/*文件输入流ifstream*/
#include <iostream>
#include <fstream> 
#include <string>
using namespace std;

int main()
{
	string path="D:\\VSCode\\ConsoleApplication1\\sunshine.txt";
	ifstream infile(path);
	if (!infile.is_open())
	{
		cout << "读取失败\n";
	}

	string buff;
	while (!infile.eof())
	{
		getline(infile,buff);
		cout << buff << endl;
	}
	infile.close();

	return 0;
}

3.和cin同时使用的问题

需要清除缓存,不然getline()无效。方法是打两遍getline()

/*因为缓存,getline()无效的问题*/
#include<iostream>
#include<string>
using namespace std;

int main() {

	//随便用cin>>输入一个东西
	int num;
	cin >> num;
	cout << num << endl;

	//getline会存在缓存问题
	string str;
	getline(cin, str);
	cout << str << endl;

	/*
	输入:12
	输出:12
	//直接结束
	*/

	return 0;
}
/*打两遍getline()清除缓存*/
#include<iostream>
#include<string>
using namespace std;

int main() {

	//随便用cin>>输入一个东西
	int num;
	cin >> num;
	cout << num << endl;

	//使用两次 getline解决缓存问题
	string str;
	getline(cin, str);
	getline(cin, str);
	cout << str << endl;

	/*
	输入:12
	输出:12
	输入:hello world
	输出:hello world
	*/

	return 0;
}

参考:
https://www.cnblogs.com/flatfoosie/archive/2010/12/22/1914055.html
http://c.biancheng.net/view/1345.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值