cin
文章目錄
- 1 通過cin讀取的數(shù)據(jù)與變量的類型不匹配
- 2 使用cin輸入時自動跳過空白字符
- 3 使用cin連續(xù)輸入多個字符串,個數(shù)不確定
- 4 使用getline從標(biāo)準(zhǔn)輸入讀入一行
- 5 使用getch()函數(shù)進(jìn)行無回顯輸入
1 通過cin讀取的數(shù)據(jù)與變量的類型不匹配
#include <iostream> #include <Windows.h> #include <string>using namespace std;int main(void) {int a;int b;int c;//std::cin >> a >> b >> c;std::cout << "請輸入a: ";std::cin >> a;if (cin.fail()) { //檢查輸入時是否發(fā)生了錯誤cout << "輸入錯誤,應(yīng)該輸入一個整數(shù)" << endl;//清除錯誤標(biāo)記,使得后續(xù)輸入可以正常進(jìn)行//但是已經(jīng)輸入的數(shù)據(jù)還在輸入緩沖區(qū)cin.clear(); cin.sync(); //清空輸入緩沖區(qū)}std::cout << "請輸入b: ";std::cin >> b;if (cin.fail()) {cout << "輸入錯誤,應(yīng)該輸入一個整數(shù)" << endl;cin.clear(); //清除錯誤標(biāo)記,使得后續(xù)輸入可以正常進(jìn)行cin.sync(); //清空輸入緩沖區(qū)}std::cout << "請輸入c: ";std::cin >> c;if (cin.fail()) {cout << "輸入錯誤,應(yīng)該輸入一個整數(shù)" << endl;cin.clear(); //清除錯誤標(biāo)記,使得后續(xù)輸入可以正常進(jìn)行cin.sync(); //清空輸入緩沖區(qū)}std::cout << "a=" << a << std::endl;std::cout << "b=" << b << std::endl;std::cout << "c=" << c << std::endl;system("pause");return 0; }cin.sync()函數(shù)在g++環(huán)境下有效,而在VS中無效,VS中可以考慮使用cin.ignore()函數(shù)。也可以手動進(jìn)行清楚緩沖區(qū)中的內(nèi)容,比如自定義一個函數(shù):
void clearBuff() {char tmp;while ((tmp = getchar()) != '\n'); }cin.ignore(count, c);
從輸入流中提取并丟棄字符,直到遇到下列三種情況:
count常常取:
- std::numeric_limits<std::streamsize>::max() , 相當(dāng)于IO流的最大字符個數(shù)
常見用法:(把標(biāo)準(zhǔn)輸入緩沖區(qū)cin的所有數(shù)據(jù)都清空)
- cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
2 使用cin輸入時自動跳過空白字符
string university; //大學(xué)string profession; //專業(yè)cout << "你是哪個學(xué)習(xí)畢業(yè)的?學(xué)什么專業(yè)? ";// 輸入: 清華 考古 hello// 自動跳過空白字符cin >> university >> profession; cout << university << "的" << profession << "專業(yè)不錯哦!" << endl;3 使用cin連續(xù)輸入多個字符串,個數(shù)不確定
#include <iostream> #include <Windows.h> #include <string>using namespace std;int main(void) {string food; //專業(yè)int i = 0;cout << "你喜歡什么美食? ";while (cin >> food) { //當(dāng)用戶輸入 Ctrl + z 并回車 cin >> food返回0, 0就是假i = i + 1;cout << "你喜歡的第" << i << "美食是:" << food << endl ;cout << "你還喜歡吃什么美食? ";}cout << "你最喜歡的美食有" << i << "種" << endl;system("pause");return 0; }4 使用getline從標(biāo)準(zhǔn)輸入讀入一行
#include <iostream> #include <Windows.h> #include <string>using namespace std;int main(void) {string addr; //專業(yè)cout << "你想到哪里去旅行:";//從標(biāo)準(zhǔn)輸入設(shè)備讀取一行,但是不包括最后輸入的回車符getline(cin, addr);//empty方法if (addr.empty() == true) {cout << "您輸入了一個空行" << endl;return 1;}//size()和length()完全等效//長度是指字符串占用的字節(jié)數(shù),如果含有漢字,那么總的字節(jié)數(shù)和漢字個數(shù)不同 cout << "地址的長度是:" << addr.size() << endl;cout << "地址的長度是:" << addr.length() << endl;system("pause");return 0; }注意: 如果在讀入一行前已經(jīng)讀取一個指定類型的數(shù)據(jù)(比如int類型、double類型等),則必須把緩沖區(qū)中回車換行符給清空掉,否則就會無法正常讀取新的一行的內(nèi)容(讀取的就是仍在緩沖區(qū)中的回車換行符)。
C++中可以使用如下代碼:
string str1, str2;cin >> str1;cin.ignore();getline(cin, str2);cout << str1 << endl;cout << str2 << endl;cin.ignore()函數(shù)的用法可以參考如下文章,寫的非常清晰易懂:一眼就能看懂的cin.ignore()函數(shù)詳解。
C語言中可以使用如下代碼:
#include <stdio.h> #include <Windows.h>int main(void) {char name[16];char addr[64];printf("姑娘芳名?\n");scanf("%s", name);//此時輸入緩沖區(qū)中還有一個回車符//清空輸入緩沖區(qū)fflush(stdin); printf("姑娘家住何地?\n");gets(addr); //讀一行,包括一行中的空格printf("家住%s 的%s, 我中意你!\n", addr, name);system("pause");return 0; }5 使用getch()函數(shù)進(jìn)行無回顯輸入
#include<conio.h> void input_pwd(char pwd[], int max_len) {char c;int i = 0;while (1) {c = getch();// getch不從輸入緩沖區(qū)中讀取//在getch中,把回車按鍵輸入,識別為回車符'\r'//在getchar中,把回車按鍵輸入,識別為換行符'\n'if (c == '\r' || i >= max_len) { pwd[i] = 0;break;}pwd[i++] = c;printf("*", c);}printf("\n"); }總結(jié)
- 上一篇: 如何开一家电动车专卖店 给感兴趣的投资者
- 下一篇: 字符集:ASCII、GB2312、GBK