?? 11-5.txt
字號:
/* 范例:11-5 */
#include <iostream.h>
#include <string.h>
#include <conio.h>
void cin_buf_clear(void); // 清除輸入緩沖區字符
void clear_state(int); // 輸出被清除字符
void view_cin_buf(void); // 檢測輸入緩沖區中的字符
void main()
{
char str1[11];
char ch,ch1,ch2;
int count,int_ch;
/* get(),get(ch) */
cout << "測試cin.get(ch).get(ch1).get(ch2)......\n";
cout << "請輸入三字符:";
cin.get(ch).get(ch1).get(ch2); // #1 無法檢測
cout << "ch=" << ch << " ch1=" << ch1 << " ch2=" << ch2 << endl;
cin_buf_clear(); // #2 清除輸入緩沖區
/* 以下比較get(),getline() */
cout << "\n比較get(str1,5,\'k\'),getline(str1,5,\'k\')===\n";
cout << "=====get(str1,5,\'k\')=======\n";
cout << "請輸入字符 abkcdef : ";
cin.get(str1,5,'k');
cout << "str1 = " << str1 << endl;
count = cin.gcount(); // 上次取的字符數
cout << "cin.gcount()=" << count << endl;
int_ch = cin.peek(); // 查看輸入緩沖區中的第一個字符
clear_state(int_ch); // #3 清除緩沖區,并輸出清除字符
cout << "\n=====getline(str1,5,\'k\')=======\n";
cout << "請輸入字符 abkcdef : ";
cin.getline(str1,5,'k');
cout << "str1 = " << str1 << endl;
count = cin.gcount(); // 上次取的字符數
cout << "cin.gcount()=" << count << endl;
int_ch = cin.peek(); // 輸入緩沖區中的第一個字符
clear_state(int_ch); // 清除緩沖區,并輸出清除字符
cout << "\nPress any key to continue...";
getche();
cin_buf_clear(); // 清除可能由getche()帶入的字符
strcpy(str1,"0123456789");
cout << "str1 = " << str1 << endl;
cout << "請輸入 abkcdef : ";
cin.read(str1,5); // 讀入5字符(不自動加'\0','\n')
cout << "cin.read(str1,5) => str1=" << str1 << endl;
view_cin_buf(); // 檢測輸入緩沖區中的字符
cout << "\nPress any key to continue...";
getche();
cin_buf_clear();
cout << "\n測試 格式標志 cin.width(5) =================\n";
cout << "請輸入 abkcdef : ";
cin.width(5); // #4 cin也可以使用width()
cin >> str1;
cout << "str1=\"" << str1 << "\"" << endl;
view_cin_buf();
cin_buf_clear(); // 清除輸入緩沖區字符
cout << "測試 格式操縱符 ws =================\n";
cout << "請輸入\" ab c d e fk\"(前置空白,tab...)\n";
cin >> ws; // #5 忽略掉前置空格符
cin.getline(str1,5);
cout << str1 << endl; // 忽略掉前置空白后,取得接下來的4個字符
getche();
}
/*************************************************************
cin_buf_clear():用來清除輸入緩沖區中第一個'\n'(含)前的字符
**************************************************************/
void cin_buf_clear()
{
while((cin.peek())!= '\n') // 將輸入緩沖區中所有字符忽略
{
cin.get();
}
cin.ignore(); // '\n'
}
/************************************************************
clear_state(char);用來顯示被清除的字符
**************************************************************/
void clear_state(int ich)
{
if (ich != '\n')
{
int int_ch;
cout << "cin.peek()=" << ich \
<< ",(即" << (char)ich << ")" << endl;
// 清除輸入緩沖區中,接下來字符...
cout << "清除 ";
for(int i=0;i<3;i++)
{
int_ch = cin.get();
cout << (char)int_ch;
}
cout << "...\n";
cin_buf_clear(); // 清除輸入緩沖區
}
}
/************************************************************
view_cin_buf();用來顯示輸入緩沖區中的字符
**************************************************************/
void view_cin_buf()
{
char ch;
char buf[100];
int count=0;
cout << "輸入緩沖區中目前字符: ";
while((ch=(char)cin.peek())!='\n')
{
cin.get(ch);
buf[count] = ch;
cout << ch;
count++;
}
// 將取出的字符,按順序再放回緩沖區中
for(int i=count-1;i>=0;i--)
cin.putback(buf[i]);
cout << endl;
}
程序執行結果︰
測試cin.get(ch),get(ch1),get(ch2)......
請輸入三字符:410
ch=4 ch1=1 ch2=0
比較get(str1,5,'k'),getline(str1,5,'k')===
=====get(str1,5,'k')=======
請輸入字符 abkcdef : abkcdef
str1 = ab
cin.gcount()=2
cin.peek()=107,(即k)
清除 kcd...
=====getline(str1,5,'k')=======
請輸入字符 abkcdef : abkcdef
str1 = ab
cin.gcount()=3
cin.peek()=99,(即c)
清除 cde...
Press any key to continue...
str1 = 0123456789
請輸入 abkcdef : abkcdef
cin.read(str1,5) => str1=abkcd56789
輸入緩沖區中目前字符: ef
Press any key to continue...
測試 格式標志 cin.width(5) =================
請輸入 abkcdef :
abkcdef
str1="abkc"
輸入緩沖區中目前字符: def
測試 格式操縱符 ws =================
請輸入" ab c d e fk"(前置空白,tab...)
ab c d e fk
ab c
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -