c语言:用户输入用户名和密码,判断是否合法用户(用户名为abc,密码为123456)

如果合法,显示"welcome to use the software",否则要求重新输入,允许输入3次,若3次都错,显示“password error!you can not use software".

1 以字符串方式,读入用户名和密码;

2 通过strcmp函数,判断是否相同。如相同,则合法。

3 给出提示信息。

说明:strcmp原型为

int strcmp(char *a, char *b);

功能为比较a和b两个字符串,如果相同返回0;如果a大返回1;如果b大返回-1。

大小依照ascii比较。


参考代码如下:

#include <stdio.h>
#include <string.h>
int main()
{
    char a[100],b[100];
    scanf("%s%s",a,b);
    if(strcmp(a,"abc") == 0 && strcmp(b, "123456") == 0) 
        printf("用户合法\n");
    else printf("用户非法\n");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-09-30
楼上的,你没进错。。不过语言是通用的。。

附楼主要的代码。

#include <stdio.h>
#include <string.h>
void main()
{
int i=0;
while(i<3)
{
printf( "输入用户名:");
char name[10],psw[10];
scanf("%s",name);
printf("输入密码:");
scanf("%s",psw);
if((strcmp(name,"abc")==0)&&(strcmp(psw,"123456"))==0)
{
printf("welcome to use the software\n");
break;
}
i++;
}
if(i>=3)
{
printf("password error!you can not use software\n");
}
}本回答被网友采纳
第2个回答  2010-09-30
楼下的,我没进错专区吧?
相似回答