1.清空控制臺窗口的輸出
<1>. 使用 system("cls");
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
// 清屏
{
for (int i=0; i<1000; ++i)
cout << i;
system("cls");
return 0;
}
<2>. 使用clrscr函數
函數名: clrscr <conio.h>
功 能: 清除文本模式窗口
用 法: void clrscr(void)
#include<stdio.h>
#include <conio.h>
int main(void)
{
int i;
clrscr();
for (i = 0; i < 20; i++)
printf("%d\r\n", i);
printf("\r\nPress any key to clear screen");
getch();
clrscr();
printf("The screen has been cleared!");
getch();
return 0;
}
2.循環使用getchar()時,若一次輸入多個字符,則會每次都會返回一個字符,直到回車,因為
字符被系統緩存了。
當你輸入一串字符時比如 abcdefg,程序會計所有的字符存放在緩存區,然后你再按回車,由于你做的是個循環啊,所以程序會把緩存區里面在遇到回車前的字符全部的調用過來,這時遇到putchar了也就一起輸出了
如果把這個程序的FOR循環改一下 改成下邊這樣的
for(;(c=getchar())!='\n';)
{
fflush(stdin);
putchar(c);
}
也就是說加上fflush(stdin); 這句話,意思是清空緩存區,這樣程序運行結果就只會輸出你輸入回車前的第一個字符 其它的字符已經被清空了
posted on 2009-03-13 11:37
李陽 閱讀(85)
評論(0) 編輯 收藏 引用