首页 > 代码库 > C++中cin流的简单使用

C++中cin流的简单使用

cin流可以用于读取标准输入流的一个字符,一行字符包含空格,多行字符等,示例如下:

#include <iostream>using std::cout;using std::endl;using std::cin;int main(void){	const int maxlength=100;	char text[maxlength]={0};	char test=0;		//读取一行文本,可以包含空格,以回车('\n')结束	cin.getline(text,maxlength);	cout << "You entered:" <<endl<<text<<endl;		//用'!'代替'\n',表示字符串的结束,这样就可以读取多行文本了	cin.getline(text,maxlength,'!');	cout << "You entered:" <<endl<<text<<endl;	//读取单个字符	cin >> test;	cout << "You entered:" << endl << test << endl;	return 0;	}