1 、使用預(yù)定義的提取符:
最一般的鍵盤輸入是將提取符作用在流類的對(duì)象 cin 上。
格式: cin>>< 表達(dá)式 >>>< 表達(dá)式 > ……
說明:⒈從鍵盤上輸入的兩個(gè) int 型數(shù)之間用空白符分隔,一般常用空白符,也可以用 tab 鍵或換行符。
2.提取符可以輸入流中讀取一個(gè)字符序列,即一個(gè)字符串。
例 4 :分析下列程序的輸出結(jié)果:
#include
#include
void main()
{
const int SIZE=20;
char buf[SIZE];
char *largest;
int curlen,maxlen=-1,cnt=0;
cout<<”Input words:\n”;
while (cin>>buf)
{
curlen=strlen(buf);
cnt++;
if(curlen>maxlen)
{
maxlen=curlen;
largest=buf;
}
}
cout< cout< cout< cout< }
執(zhí)行如下: Input words:
if else return do while switch case for goto break continue (Ctrl+Z)
2 、使用成員函數(shù) get() 獲取一個(gè)字符:
格式: cin.get(char &ch)
功能:可以從輸入流獲取一個(gè)字符,并把它放置到指定變量中。
例 5 :當(dāng)從鍵盤上輸入如下字符序列時(shí),分析輸出結(jié)果:
abc xyz 123
#include
void main()
{
char ch;
cout<<”input:”;
while((ch=cin.get())!=EOF)
cout.put(ch);
cout<<”O(jiān)K!”;
}
注解 :⒈ EOF 是個(gè)符號(hào)常量,其值為 -1 。
⒉ get() 函數(shù)還可以在程序中這樣使用:
char ch;
cin.get(ch); // 從輸入流中讀取一個(gè)字符存放在 ch 中。
Cout.put(ch); //ch 中的字符顯示在屏幕上。
⒊ getline(): 功能是可以從輸入流中讀取多個(gè)字符。
格式: cin.getline(char *buf,int limit ,deline='\n');
其中: (1)buf 是一個(gè)字符指針或一個(gè)字符數(shù)組。
(2)limit 用來限制從輸入流中讀取到 buf 字符數(shù)組中的字符個(gè)數(shù),最多只能讀 limit-1 個(gè),留 1 個(gè)放結(jié)束符。
(3)deline 讀取字符時(shí)指定的結(jié)束符。
例 5 :編程統(tǒng)計(jì)從鍵盤上輸入每一行字符的個(gè)數(shù),從中選出最長(zhǎng)的行的字符個(gè)數(shù),統(tǒng)計(jì)共輸入多少行?
#include
const int SIZE=80;
void main()
{
int lcnt=0,lmax=-1;
char buf[SIZE];
cout<<”Input ……..\n”;
while (cin.getline(buf,SIZE))
{
int count=cin.gcount();
lcnt++;
if(count>lamx) lamx=xount;
cout<<”Line#”< cout.write(buf,count).put(‘\n').put(‘\n');
}
cout< cout<<”Total Line:”< cout<<”Longest Line:”< }
3 、使用成員函數(shù) read() 讀取一串字符:
格式: cin.read(char *buf,int SIZE)
功能:從輸入流中讀取指定的數(shù)目的字符,并將它們存放在指定的數(shù)組中。
例 6 : #include
void main()
{
const int S=80;
char buf[S]=” “;
cout<<”Input…..\n”;
cin.read(buf,S);
cout< cout< }
Input…..
Abcd
Efgh
Ijkl
輸出: abcd
efgh
ijkl
最一般的鍵盤輸入是將提取符作用在流類的對(duì)象 cin 上。
格式: cin>>< 表達(dá)式 >>>< 表達(dá)式 > ……
說明:⒈從鍵盤上輸入的兩個(gè) int 型數(shù)之間用空白符分隔,一般常用空白符,也可以用 tab 鍵或換行符。
2.提取符可以輸入流中讀取一個(gè)字符序列,即一個(gè)字符串。
例 4 :分析下列程序的輸出結(jié)果:
#include
#include
void main()
{
const int SIZE=20;
char buf[SIZE];
char *largest;
int curlen,maxlen=-1,cnt=0;
cout<<”Input words:\n”;
while (cin>>buf)
{
curlen=strlen(buf);
cnt++;
if(curlen>maxlen)
{
maxlen=curlen;
largest=buf;
}
}
cout<
執(zhí)行如下: Input words:
if else return do while switch case for goto break continue (Ctrl+Z)
2 、使用成員函數(shù) get() 獲取一個(gè)字符:
格式: cin.get(char &ch)
功能:可以從輸入流獲取一個(gè)字符,并把它放置到指定變量中。
例 5 :當(dāng)從鍵盤上輸入如下字符序列時(shí),分析輸出結(jié)果:
abc xyz 123
#include
void main()
{
char ch;
cout<<”input:”;
while((ch=cin.get())!=EOF)
cout.put(ch);
cout<<”O(jiān)K!”;
}
注解 :⒈ EOF 是個(gè)符號(hào)常量,其值為 -1 。
⒉ get() 函數(shù)還可以在程序中這樣使用:
char ch;
cin.get(ch); // 從輸入流中讀取一個(gè)字符存放在 ch 中。
Cout.put(ch); //ch 中的字符顯示在屏幕上。
⒊ getline(): 功能是可以從輸入流中讀取多個(gè)字符。
格式: cin.getline(char *buf,int limit ,deline='\n');
其中: (1)buf 是一個(gè)字符指針或一個(gè)字符數(shù)組。
(2)limit 用來限制從輸入流中讀取到 buf 字符數(shù)組中的字符個(gè)數(shù),最多只能讀 limit-1 個(gè),留 1 個(gè)放結(jié)束符。
(3)deline 讀取字符時(shí)指定的結(jié)束符。
例 5 :編程統(tǒng)計(jì)從鍵盤上輸入每一行字符的個(gè)數(shù),從中選出最長(zhǎng)的行的字符個(gè)數(shù),統(tǒng)計(jì)共輸入多少行?
#include
const int SIZE=80;
void main()
{
int lcnt=0,lmax=-1;
char buf[SIZE];
cout<<”Input ……..\n”;
while (cin.getline(buf,SIZE))
{
int count=cin.gcount();
lcnt++;
if(count>lamx) lamx=xount;
cout<<”Line#”<
}
cout<
3 、使用成員函數(shù) read() 讀取一串字符:
格式: cin.read(char *buf,int SIZE)
功能:從輸入流中讀取指定的數(shù)目的字符,并將它們存放在指定的數(shù)組中。
例 6 : #include
void main()
{
const int S=80;
char buf[S]=” “;
cout<<”Input…..\n”;
cin.read(buf,S);
cout<
Input…..
Abcd
Efgh
Ijkl
輸出: abcd
efgh
ijkl

