c++ 读入一行字符串(包括空格)

一、 gets

#include <cstdio>
#include <iostream> 
using namespace std;
int main() {
	char s[1000];
	gets(s); 
	cout << s << endl;
	return 0;
}

运行结果:
在这里插入图片描述

  • 若在gets上面有整数输入,那么必须再写一次gets() 吸收换行。

二、cin.getline()

#include <cstdio>
#include <iostream> 
using namespace std;
int main() {
	char s[1000];
	cin.getline(s, 1000); 
	cout << s << endl;
	return 0;
}

运行结果:
在这里插入图片描述

发布了414 篇原创文章 · 获赞 380 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104239984