C++读取一行字符串输入

这个知识点必须掌握,因为是必备技能!不然连输入都搞不定,何谈后面的?

#include <iostream>
#include <string>
using namespace std;

int main()
{
	//对于char* / char[]
	char s[1001];
	cout<<"Please input char[]:"<<endl;
	cin.getline(s, 1000);//iostream下的函数, 第二个参数表示允许的最大输入长度
	cout<<"Output:"<<endl<<s<<endl<<strlen(s)<<endl; 

	//对于string
	string ss;
	cout<<"Please input string:"<<endl;
	getline(cin, ss); //这个getline函数在<string>头文件下
	cout<<"Output:"<<endl<<ss<<endl<<ss.length()<<endl;

	return 0;
}
/**
输入和输出样例:
Please input char[]:
He llo
Output:
He llo
6
Please input string:
Wor ld
Output:
Wor ld
6
*/

posted @ 2016-09-23 21:35  Victor_Lv  阅读(977)  评论(0编辑  收藏  举报