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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章