题目:
从键盘输入6位仅由数字0~9组成的密码。用户每输入一个密码并按回车键后,程序给出判断:如果是数字,则原样输出该数字,并提示用户目前已经输入了几位密码,同时继续输入下一位密码;否则,程序提示"error",并让用户继续输入下一位密码。直到用户输入的密码全部是数字为止。
我的代码:
#include
int main()
{
int i=1;
while(i<=6)
{
char m;
printf("Input your password:\n");
scanf("%c",&m);
if(m>='0'&&m<='9')
{
printf("%c, you have enter %d-bits number\n",m,i);
i++;
}
else
printf("error\n");
goto printf("Input your password:\n");
scanf("%c",&m);
}
return 0;
}
那么问题来了,究竟那里面出了问题
else最好加括号,
另外goto那句和紧跟goto的那句要删掉,没有用还写的不对。
将while循环的最后两行删掉。
参考这个:
#include <stdio.h>
int main()
{
int num;
char c;
num = 0;
printf("Input your password:\n");
while (num< 6)
{
scanf("%c%*c", &c);//%*c用来过滤掉回车符
if (c >= '0'&&c <= '9')
{
num++;
printf("%c, you have enter %d-bits number\n", c, num);
}
else printf("error\n");
}
}