多用户登录系统C语言程序

如图,题目二

#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "windows.h"

int total=0;

struct u_p
{
char user[20];
char pass[20];
} s[50];

void read()
{
total=GetPrivateProfileInt("INFO","count",0,"d:\\Info.dat");
int i;
char t[5]={"\0"};
for(i=0;i<total;i++)
{
sprintf(t,"%d",i+1);
GetPrivateProfileString(t,"USER","",s[i].user,20,"d:\\Info.dat");
GetPrivateProfileString(t,"PASSWORD","",s[i].pass,20,"d:\\Info.dat");
}
}

void input()
{
int p,i=0,count=0,f_u=0,f_p=0;
char user[20]={"\0"};
char password[20]={"\0"};
while(1)
{
f_u=0;
f_p=0;
system("cls");
printf("当前共有%d个注册用户",total); 
printf("\n\n请输入用户名:");
memset(user,'\0',20);
scanf("%s",user);
printf("\n请输入密码:");
memset(password,'\0',20);
i=0;
while(1)
{
p=_getch();
if(p==10 || p==13)
{
break;
}
password[i++]=p;
printf("*");
}
for(i=0;i<total;i++)
{
if(strcmp(s[i].user,user)==0)
{
f_u=1;
if(strcmp(s[i].pass,password)==0)
{
f_p=1;
printf("\n\n欢迎 %s",user);
fflush(stdin);
_getche();
continue;
}
}
}
if(f_u==0)
{
printf("\n\n不存在该用户名! 选 1 重新输入,选 2 注册新用户");
int c=0;
fflush(stdin);
c=_getche();
if(c=='1')
{
continue;
}
else if(c=='2')
{
system("cls");
printf("注册新用户");
printf("\n\n\n请输入用户名:");
memset(user,'\0',20);
scanf("%s",user);
printf("\n请输入密码:");
char temp[20]={"\0"} ;
i=0;
while(1)
{
p=_getch();
if(p==10 || p==13)
{
break;
}
temp[i++]=p;
printf("*");
}
printf("\n请再次输入密码:");
i=0;
memset(password,'\0',20);
while(1)
{
p=_getch();
if(p==10 || p==13)
{
break;
}
password[i++]=p;
printf("*");
}
if(strcmp(temp,password)==0)
{
total++;
char t[5]={"\0"};
sprintf(t,"%d",total);
WritePrivateProfileString("INFO","count",t,"d:\\Info.dat");
WritePrivateProfileString(t,"USER",user,"d:\\Info.dat");
WritePrivateProfileString(t,"PASSWORD",password,"d:\\Info.dat");
printf("\n\n注册成功,请重新登录");
fflush(stdin);
_getch();
count=0;
read();
continue; 
}
else
{
printf("\n\n两次密码不一致,注册失败");
fflush(stdin);
_getch();
count=0;
continue; 
}
}
}
else if(f_p==0)
{
count++; 
if(count>=3)
{
printf("\n\n连续输入3次错误,程序将退出");
fflush(stdin);
_getche();
return ; 
}
printf("\n\n密码输入错误,请重新输入");
fflush(stdin);
_getche();
}
}
return ;
}

int main(int argc, char *argv[]) 
{
read();
input();

return 0;
}

追问

为什么我显示有错啊?我用的是VC

追答

vc里面啊, 要加头文件 #include "conio.h"

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-12-30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct usr
{
    char name[100];
    char password[100];
    struct usr *next;
}USR;
USR *head=NULL;
int main(void)
{
    int a=0;
    do
    {
        printf("请选择操作:\n1:登陆\n2:注册\n其余按键退出\n");
        scanf("%d",&a);
        if(a==1)
            LogIn();
        else if(a==2)
            Register();
    }while(a==1||a==2);
    if(head)
        FreeHead();
    return 0;
}
void LogIn()
{
    USR *now;
    char pass[100],nam[100];
    int i;
    for(i=0;i<5;i++)
    {
        memset(pass,0x00,sizeof(pass));
        memset(nam,0x00,sizeof(nam));
        printf("请输入用户名:\n");
        scanf("%s",nam);
        printf("请输入密码:\n");
        scanf("%s",pass);
        now=head;
        while(now)
        {
            if((strcmp(nam,now->name)==0)&&(strcmp(pass,now->password)==0))
            {
                printf("登陆成功\n");
                return;
            }
            now=now->next;
        }
        printf("账户或密码错误\n");
    }
    printf("错误次数过多, 请稍后重试\n");
    return;
}
void Register()
{
    USR *tmp,*now=head;
    tmp=(USR*)calloc(1,sizeof(USR));
    printf("请输入用户名:\n");
    scanf("%s",tmp->name);
    while(now)
    {
        if(strcmp(tmp->name,now->name)==0)
        {
            printf("该用户名已注册\n");
            free(tmp);
            return;
        }
    }
    printf("请输入密码:\n");
    scanf("%s",tmp->password);
    tmp->next=head;
    head=tmp;
    return;
}
void FreeHead()
{
    USR now;
    now=head;
    while(now)
    {
        head=now->next;
        free(now);
        now=head;
    }
    return ;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网