📄 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 + -