cin
輸入
使用cin當輸入指令
cin輸入數字資料型態
一、輸入
語法:std::cin >> 變數名稱;
程式碼
#include<iostream>
main(){
int x;
std::cin >> x >> endl;
return 0;
}
Input: 123、Output: 123
※每次用到cin時,前面都必須加上std::,嫌加上std::太麻煩,那就引用命名空間。
二、引用命名空間std
語法:using name space std;
cin >> 變數名稱;
程式碼
#include<iostream>
using namespace std;
main(){
int x;
cin >> x;
cout << x << endl;
return 0;
}
Input: 456、Output: 456
※cin遇空白、Enter、Tab字元即停止讀入。
三、多變數輸入
語法:cin >> 變數1 >> 變數2;
※空白、Enter、非數值字元停止讀入。
Ex:cin同時輸入兩整數 ,輸出合計值。(等級:易)
#include<iostream>
using namespace std;
main(){
int x,y;
cin >> x >> y;
cout << x+y << endl;
return 0;
}
Input: 123 456、Output: 579
Input: 123abc 456、Output: 123、Q:456不見了?
※解決方法:分兩次輸入,兩次之間先清除buffer、以Enter區隔。
#include<iostream>
using namespace std;
main(){
int x,y;
cin >> x;
fflush(stdin);
cin >> y;
cout << x+y << endl;
return 0;
}
※考慮輸入時可能多出的字元,第二個cin之前先清buffer,即能解決並正確讀入資料。
cin輸入字元資料型態
一、輸入字元
語法:cin >> 字元變數;
#include<iostream>
using namespace std;
main(){
char ch;
cin >> ch;
cout << ch << endl;
return 0;
}
Input: ABC、Output: A
※會讀入一個字元。(剩餘的字元會留在buffer內)
cin輸入字串資料型態
一、輸入字串
語法:cin >> 字串變數;
#include<iostream>
using namespace std;
main(){
char str[10];
cin >> str;
cout << str << endl;
return 0;
}
Input: ABC DEF、Output: ABC
※cin遇空白、Tab、Enter停止讀入。
FIFO先進先出
字元緩衝區入口
字元緩衝區出口
清除cin字元緩衝區
語法:fflush(stdin);
可移除上一次輸入過多的字元以及Enter。
cin讀空白字元
cin.get()、cin.getline()、cin.gets()
參考網址:https://dotblogs.com.tw/v6610688/2013/10/23/cin_getline_empty_newline_problem
一、cin.get()
遇Enter停止,同時將Enter也讀入。
語法:cin.get(string_name, size);
#include<iostream>
using namespace std;
main(){
char str[20];
cin.get(str,10);
cout << str;
return 0;
}
input: This is a book.
output: This is a
解析:get()會讀入指定長度的字元,或是讀到Enter為止(若Enter在指定長度內會一併讀入)。
str的內容: [T][h][i][s][ ][i][s][ ][a][\0],最後為\0。
練習題
範例:使用cin.get(),讀入姓名與身分證。
char name[10],id[10];
cout << "輸入姓名:";
cin.get(name,10);
cout << "輸入身分證:";
cin.get(id,10);
cout << "姓名:" << name << endl;
cout << "ID:" << id << endl;
Input: Allen Chang S121155891
Output:
姓名:Allen Chang[\0] (共計12個字元)
ID:S121155891[\0] (共計11個字元)
※輸入太短字串,例如:Allen<Enter>,則
Output:
姓名:Allen[\0] (共計6個字元)
ID:<Enter>[\0] (Enter會存入,共計2個字元)
改良方式,不要讓Enter被存入ID:
char name[10],id[10];
cout << "輸入姓名:";
cin.get(name,10).get();
cout << "輸入身分證:";
cin.get(id,10);
cout << "姓名:" << name << endl;
cout << "ID:" << id << endl;
※輸入時必須按兩次Enter。
範例:輸入20個字元以內的字元,超過部分捨棄。
#include<iostream>
#include<cstring>
using namespace std;
main(){
char str[20];
cout << "請輸入長度不超過20的字串:";
cin.get(str,20); //使用get()不會濾掉空白
cout << str << endl;
system("pause");
return 0;
}
二、cin.getline()
遇Enter停止讀取,不會將Enter讀入。
語法:cin.getline(string_name, size);
範例:使用cin.getlin(),讀入姓名。
char name[10];
cout << "輸入姓名:";
cin.getline(name,10); //讀入的字串最大長度為10個字元
Input: Allen Chang
Output: Allen Cha
解析:讀入前9個字元、最後面是字串結束字元null。 (null = \0)
name的內容: [A][l][l][e][n][ ][C][h][a][\0],最後為\0。
範例:先cin.get()、再cin.getline(),且輸入一次超過10個以上字元後,按Enter結束輸入。
#include<iostream>
using namespace std;
main(){
char str[20],str1[20];
cin.get(str,10);
cout << str;
cin.getline(str1,10);
cout << str1 << endl;
return 0;
}
input:
This is a book.<Enter>
output:
This is a
book.
解析:get()沒有讀到Enter,Enter會被getline()當成結束字元、沒有讀到str1裡。
範例:先cin.getline()、再cin.get(),且輸入一次超過10個以上字元後,按Enter結束輸入。
#include<iostream>
using namespace std;
main(){
char str[20],str1[20];
cin.getline(str1,10);
cout << str1;
cin.get(str,10);
cout << str << endl;
return 0;
}
input:
This is a book.
output:
This is a
解析:str沒有字元?(實際上str裡存了一個Enter)
三、cin.gets()
cin.gets()於c11之後已經被刪除,建議不要使用,因為不安全。