带空格的字符串输入处理

1.若输入为string类型,可用getline(),注意必须带string头文件

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s;
	while (getline(cin,s))
		cout << s << endl;
	return 0;
}

2.若输入为char类型,可用cin.get()

#include<iostream>
#include<string>

using namespace std;

int main()
{
	char s[10];
	while (cin.get(s, 10).get())
		cout << s << endl;
	return 0;
}

或者cin.getline()

#include<iostream>
#include<string>

using namespace std;

int main()
{
	char s[10];
	while (cin.getline(s, 10))
		cout << s << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40736096/article/details/80214803
今日推荐