这个小程序有个很奇怪的问题高手进
while(ch=getch())
{
if(isalnum(ch))
{
temp[i]=ch;
printf("*");
i++;
}
else if(ch=='\b'){printf("\b \b");i--;}
else if(ch=='\r'){break;}
if(i>21){printf("length of your input is not proper,please check it\n");login(password);}
}
之前定义了temp[100],包括了该有头文件,请问下大家为什么按del键,和按方向键也会当成输入呢,就是说,按了del键,屏幕上会输出一个*,明明他不是字母或数字
[解决办法]
你谁说del不是字符?你代码遇到字符就输星号,没问题啊
[解决办法]
你这个程序没错,输出结果也没错。只是你对方向键的键值不清楚而已。
我测试了下,方向键(值):
向左:75:K
向上:72:H
向右:77:M
向下:80:P
所以你按这几个键当然会使isalnum为true了!
看看这样的测试代码就清楚了:
#include <stdio.h>#include <conio.h>#include <ctype.h>int main(){ char ch, temp[100] = {0}; int i = 0; while(ch=getch()) { if(isalnum(ch)) { temp[i]=ch; printf("*%d", ch); i++; } else if(ch=='\b'){printf("\b \b");i--;} else if(ch=='\r'){break;} if(i>21) { printf("length of your input is not proper,please check it\n"); //login(password); } } printf("temp = [%s]%d\n", temp, '\r'); return 0;}
[解决办法]
再补充下:del键对应的是整数83,即字符S。
因为:KHMPS都是字母,所以isalnum就返回true了!
[解决办法]