输入密码的问题,结果总是密码错误。
前言:刚学了字符数组,并按照书上的例题,写了个输入密码的程序。症结:无论输入正确密码,还是输入错误密码,结果总显示的是“错误密码”。
希望高手指点!
具体程序:
# include
# define N 6
void main()
{
char code[N] = {'h', 'e', 'l', 'l', 'o'};
char user[N];
int i, k, t = 0;
while (1)
{
k = 1;
printf ("\n");
printf ("Please input your key: ");
for (i = 0; i < N; i ++)
user[i] = getchar ();
for (i = 0; i < N; i ++)
{
if (code[i] != user[i])
{
k = 0;
break;
}
}
t ++;
if (k == 0)
{
printf ("\n Key Error!");
}
else
{
printf ("\n Welcome!");
break;
}
if (t >= 3)
{
printf ("\n Your chances are not left, so you'll exit!");
break;
}
}
}