用c语言设计一个用户登录软件,求源代码

实现功能:
1) 提示输入用户名和密码(初始用户名为:guest;密码为:123),对输入信息进行检测;正确则提示欢迎信息,错误则提示重新输入(错误三次,退出系统)。
2) 提供密码的修改功能,输入原先的密码,正确则可以修改;输入新设定的密码,提示“再输入一遍”新设定的密码,2次一致则修改成功,否则提示有误,并允许重新设定或退出。
最新回答
键盘书生

2025-03-02 06:14:52

void student::print()
{
char s[10];
int i=0;
string password="guest";
cout<<endl<<endl<<endl;
cout<<"\t\t**************************************\n";
cout<<"\t\t***** 学生信息管理系统 *****\n";
cout<<"\t\t**************************************\n";
cout<<"\t\t请输入密码(7位数):";
s[0]=getch();
while(s[i]!='\r') //输入回车键停止
{
cout<<"*";
s[++i]=getch();
}
s[i]='\0';
cout<<endl;
if(s==password)
cout<<"\t\t欢迎使用学生信息管理系统!\n";
else
{
cout<<"\t\t您输入的密码有误!无权使用\n";
exit(0);
}
system("cls");
}
天天

2025-03-02 07:56:04

FYI
code:

#include <stdio.h>
#include <string.h>
typedef struct
{
char name[100];
char pw[100];
}uifo;
#define USER_FILE "data.bin"
#define DEFAULT_NAME "guest"
#define DEFAULT_PW "123"
void write_data(uifo *info)
{
unsigned char len;
FILE *fp;
fp = fopen(USER_FILE, "wb");
if(fp == NULL)
{
printf("can not open file \n");
return;
}
len = strlen(info->name);
fwrite(&len, 1, 1, fp);
fwrite(info->name, 1, len, fp);
len = strlen(info->pw);
fwrite(&len, 1, 1, fp);
fwrite(info->pw, 1, len, fp);
fclose(fp);
}
void read_data(uifo *info)
{
FILE *fp;
unsigned char len;
fp = fopen(USER_FILE, "rb");
if(fp == NULL)
{
strcpy(info->name, DEFAULT_NAME);
strcpy(info->pw, DEFAULT_PW);
write_data(info);
}
else
{
memset(info, 0, sizeof *info);
fread(&len, 1, 1, fp);
fread(info->name, 1, len, fp);
fread(&len, 1, 1, fp);
fread(info->pw, 1, len, fp);
fclose(fp);
}
}
int main()
{
uifo info;
char pw[100]={0}, pw_confirm[100]={0};
int wt = 0;

memset(&info, 0, sizeof info);
read_data(&info);
printf("welcome! please login\n");
while(1)
{
printf("please input your user name\n");
scanf("%s", pw);
if(strcmp(pw, info.name) != 0)
{
printf("no such user name\n");
continue;
}
printf("please input your password\n");
scanf("%s", pw);
if(strcmp(pw, info.pw) != 0)
printf("password error\n");
else break;
wt ++;
if(wt >= 3)
{
printf("3 times password error, exit!\n");
return -1;
}
}
while(1)
{
int i;

printf("input 1 to change your password\ninput 0 to exit\n");
scanf("%d", &i);
if(i == 0) break;
if(i == 1)
{
while(1)
{
printf("please input your original password\n");
scanf("%s", pw);
if(strcmp(pw, info.pw) != 0)
{
printf("original password is not correct\n");
continue;
}
printf("please input your new password\n");
scanf("%s", pw);
printf("please confirm your new password\n");
scanf("%s", pw_confirm);
if(strcmp(pw, pw_confirm) != 0)
{
int j;
printf("the new passwords are not same in two times input!\n");
while(1)
{
printf("input 1 to change password again\ninput 2 to back to last step\ninput 0 to exit\n");

scanf("%d", &j);
if(j == 0) return -2;
if(j == 1 || j == 2) break;
printf("unknown input\n");
}
if(j == 2) break;
continue;
}
strcpy(info.pw, pw);
write_data(&info);
break;
}
}
else printf("unknown input, should be 0/1\n");
}
printf("Bye!\n");
return 0;
}