C++学习之使用cin进行输入

基础

  1. cin对象将标准输入表示为字节流,能够识别大部分的基本类型。
  2. 可以将hex,oct,和dec控制符与cin一起使用,来指定将整数输入解释为十六进制、八进制还是十进制。例如cin>>hex;
  3. 对于char* 类型的参数,抽取运算符读取输入中的下一个单词,将它放置在指定的地址,并加上一个空值字符,使之成为一个字符串。例如:
cout<<"Enter your name: \n";
char name[5];
char groud[10];
cin>>name>>groud;//如果输入大于5个字符,name自动扩长

不同版本的抽取运算符查看输入流的方法是相同的。它们跳过空白(空格,换行符和制表符),直到遇到非空白字符。
4. 如果希望输入在异常后能够读取后面的输入,就必须将流状态重置为良好,这可以通过clear()方法来实现:

#include<iostream>
using namespace std;
int main() {
	int input,sum = 0;
	while (cin >> input) {
		sum += input;
	}
	cout << "Last value = " << input << endl;
	cout << "sum=" << sum << endl;
//--------------2-----------------------
	//只能跳过一个异常输入
	if (cin.fail() && !cin.eof()) {//fail because of mismatched input
		cin.clear();//reset stream state
		while (!isspace(cin.get()))
			continue;//get rid of bad input
	}
	else {
		cout << "I cant go on!\n";
		exit(1);
	}
	cout << "Now enter a number: ";
	cin >> input;
	cout << input;
------------------1不恰当输入终止-----------------
	//cin.clear();
	//while (!isspace(cin.get())) {//isspace()函数一直读取字符,直到到达空白为止
	//	continue;
	//}
	//cin >> input;
	//cout << "L:" << input;
	system("pause");
	return 0;
}

isspace()方法是一直读取字符,直到到达空白字符为止。程序中当输入字符时存在输入异常。

其它istream类方法

  • 方法get(char&)和get(void)提供不跳过空白的单字符输入功能,cin>>char会跳过空白;
  • 函数get(char*,int,char)和getline(char*,int,char)在默认情况下读取整行而不是一个单词。
    1.成员函数get(void)
	int ct = 0;
	char ch;
	cin >> ch;
	//cin.get(ch);
	while (ch != '\n') {
		cout << ch;
		ct++;
		1 跳过空格和换行符
		//cin >> ch;
		//if (ch == '0') //cin跳出循环
			//break;
		//2 get运算跳过换行符,但不跳过空格。
		//cin.get(ch);
		//3
		ch = cin.get();
	}
	cout << ct << endl;

cin.get()返回char类型,cin.get(ch)将char赋值给ch返回cin,所以cin.get(a).get(b)合理,cin.get()不能连续get。
2. 采用哪种单字符输入形式
例子:清空缓冲行

	cout << "Enter a,b,c,d or q: ";
	char ch;
	cin >> ch;
	while (ch != 'q') {
		switch (ch) {
		case 'a':cout << 90 << endl;break;
		case 'b':cout << 80 << endl;break;
		case 'c':cout << 70 << endl;break;
		case 'd':cout << 60 << endl;break;
		default:break;
		}
		cout << "Enter a,b,c,d or q: ";
		cin.clear();//清空缓冲区前必须先clear()
		cin.ignore(numeric_limits<std::streamsize>::max(),'\n'); //清除输入缓冲区的当前行
		cin >> ch;
	}

3.字符串输入
istream& get(char* ,int, char)
istream& get(char* ,int)
istream& getline(char* ,int, char)
istream& getline(char* ,int)
第一个参数是用于放置输入字符串的内存单元地址。第二个参数要比读取的最大字符数大1(用于存储换行符)。第三个参数指定用作分界符的字符。
区别: get()将换行符留在输入流中,这样接下来的输入操作首先看到的是换行符,而getline()抽取并丢弃输入流中的换行符。分界字符同理,get()的分界字符仍在输入流中。
ignore()接受两个参数:一个是数字,指定要读取的最大字符数;另一个是字符,用作输入分界符。
例: cin.ignore(25,’\n’).ignore(25,’\n’)
第一个ignore()读取并丢弃一行,第二个调用并丢弃另一行。
具体例子

#include<iostream>
using namespace std;
const int Limit = 255;
int main() {
	//getline()函数将丢弃输入中的分界字符#,而get()函数不会。
	char input[Limit];
	cout << "Enter a string for getline() processing:\n";
	cin.getline(input, Limit, '#');
	cout << "Here is your input:\n";
	cout << input << "\nDone with phase 1\n";
	
	char ch;
	cin.get(ch);
	cout << "The next input character is " << ch << endl;

	if (ch != '\n')
		cin.ignore(Limit, '\n');//discard rest of line

	cout << "Enter a string for get() processing:\n";
	cin.get(input, Limit, '#');//三个输入参数
	cout << "Here is your input:\n";
	cout << input << "\nDone with phase 2\n";

	cin.get(ch);
	cout << "The next input character is " << ch << endl;

	system("pause");
	return 0;
}

4.其它istream方法
read()函数读取指定数目的字节,并将它们存储在指定的位置中。但是不会在输入后加上空值字符,因此不能将输入转换为字符串。read方法不是专门为键盘输入设计的,常用于文件形式。
peek()函数返回输入中的下一个字符,但不抽取输入流中的字符。
gcount()函数返回最后一个非格式化抽取方法读取的字符数。这意味着字符是由get(),getline(),ignore()或read()方法读取的,不是由抽取运算符(>>)读取的。但是使用strlen()函数速度更快
putback()函数****将一个字符插入到输入字符串中,被插入的字符将是下一条输入语句读取的第一个字符。
具体例子

char ch;
	while (cin.get(ch)) {
		if (ch != '#')
			cout << ch;
		else {
			cin.putback(ch);//reinsert character
			break;

		}
	}
	if (!cin.eof()) {
		cin.get(ch);
		cout << endl << ch << " is next input character.\n";
	}
	else {
		cout << "End of file reached.\n";
		exit(0);
	}
	while (cin.peek() != '#') {//peek查看,但不抽取输入流中的字符.
		cin.get(ch);
		cout << ch;
	}
	if (!cin.eof()) {
		cin.get(ch);
		cout << endl << ch << "is next input character.\n";
	}
	else {
		cout << "End of file reached.\n";
	}

另一个例子,使用peek()来确定是否读取了整行。如果一行中只有部分内容被加入到输入数组中,程序将删除余下内容。

#include<iostream>
const int SLEN = 10;
using namespace std;
inline void eatline() {
	while (cin.get() != '\n')
		continue;
}
int main() {
	char name[SLEN];
	char title[SLEN];
	cout << "Enter you name: ";
	cin.get(name, SLEN);
	if (cin.peek() != '\n')
		cout << "Sorry we only have enough room for " << name << endl;
	eatline();
	cout << "Dear " << name << ", enter your title:\n";
	cin.get(title, SLEN);
	if (cin.peek() != '\n')
		cout << "We were forced to truncate your title.\n";
	eatline();
	cout << "name: " << name << "\ntitle: " << title << endl;
	system("pause");
	return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值