C语言编程:实现用户的注册和登录

编写程序,模拟用户注册和登录的过程,登录时要进行身份验证。用户注册时输入用户名和密码,密码要求连续输入两次且完全一样,注册成功后给予“注册成功!”的提示信息,如连续输入三次,密码都不同,则提示“注册失败”。登录时需要输入用户名和密码,如果没有注册过用户,提示"不能登录,用户未注册!”, 如果输入用户名不正确,提示"用户名不对”,并请重新输入,若连续试验三次都不对,则提示“登录不成功!”,否则继续输入用户密码,密码输入正确,提示“登录成功”,若连续输入密码三次都不争取,则提示“登录不成功!”。运行界面如图所示好好看图

模拟用户注册和登陆可以用文件来保存用户名和密码。注册就是向文件里写,用if判断两次密码是否一致。连续三次,可以有一个变量,每次输入加一,变量大于三就提示登陆不成功。用户名不对,那你就把你输入的用户名和文件里的用户名是否一致。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-12-11
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

bool search(char id[], char pass[]) {

*fp;
char tid[10], tpass[10];
fp = fopen("c:\\data", "r");
while (!feof(fp)) {
fscanf(fp, "%s%s", tid, tpass);
if (
(tid, id)==0 &&
(tpass, pass)==0) {
fclose(fp);
return true;
}
}
fclose(fp);
return false;
}

bool login() {
char id[10], pass[10];
printf("Login\nPress the id: ");
scanf("%s", id);
printf("Press the password: ");
// 可以自行将password处理成*号, 如果不会可以发信给我
scanf("%s", pass);
printf("-----------------------");
if (search(id, pass))
return true;
else
return false;
}

void _add(char id[], char pass[]) {

*fp;
fp=fopen("c:\\data", "a");
// 在写入文件时可以按一定的排序方式插入,可减少以后Login时的search时间
fprintf(fp, "%s %s\n", id, pass);
fclose(fp);
}

void regis() {
char id[10], pass[10], tpass[10];
printf("Register\nPress the id: ");
scanf("%s", id);
while (true) {
printf("Press the password: ");
scanf("%s", pass);
printf("Press the password again: ");
scanf("%s", tpass);
if (
(pass, tpass) != 0)
printf("The passwords you pressed are not the same!\n");
else
break;
}
_add(id, pass);
printf("-----------------------Register successfully!\n");
}

void init() {

*fp;
if ((fp=fopen("c:\\data", "r")) ==
) { // 注意,一定要有个名叫data(没有
)的合法文件在C盘

printf("---------File is not exist\n");
system("pause");
exit(0);
}
else
fclose(fp);
}

int main(void){
int command;
init(); // 检查data文件在不在
while (true) {
printf("-----------------------(Login: 1 Register: 2 Exit: 3)\n");
scanf("%d", &command);
printf("-----------------------\n");
// 这里可以编写command的检测语句
if (command == 3)
break;
else if (command == 1) {
if (!login())
printf("ID is not exist or password is wrong!\n");
else
printf("Login successfully!\n");
}
else
regis();
}
return 0;
}
第2个回答  2011-12-22
#include "stdafx.h"
#include "string.h"
#define n 20

void zhuce();
void denglu();
char yhm[n],mm[n];
int main(int argc, char* argv[])
{
int i;

printf("-----------\n1.注册\n2.登陆\n3.继续\n0.退出\n");
scanf("%d",&i);
switch(i)
{case 0: break;
case 1 : zhuce();break;
case 2: denglu();break;

}

return 0;
}
void zhuce( )
{char temp1[n],temp2[n],temp3[n],yhmtmp[n];

printf("输入用户名\n");
fflush(stdin);//清空缓存
gets(yhmtmp);

printf("输入密码\n");
fflush(stdin);
gets(temp1);
printf("输入密码确认\n");
fflush(stdin);
gets(temp2);
if(!strcmp(temp1,temp2))
{strcpy(mm,temp1);
printf("注册成功\n");

}
else
{printf("输入密码确认\n");
gets(temp3);
if(!strcmp(temp1,temp3))
{strcpy(mm,temp1);
printf("注册成功\n");

}
else

printf("注册失败\n");

}

}
void denglu( )
{
char s1[n],s2[n];
printf("输入用户名\n");
fflush(stdin);
gets(s1);
printf("输入密码\n");
fflush(stdin);
gets(s2);
if((strcmp(s1,yhm))&&(strcmp(s2,mm)))
printf("登陆成功\n");

}

还没实现在一次运行中及注册又可以登陆,所以你要登陆的话第二次运行此程序追问

运行不了啊

追答

?可以的

本回答被提问者和网友采纳
第3个回答  2011-12-25
#include "stdafx.h"
#include "string.h"
#define n 20

void zhuce();
void denglu();
char yhm[n],mm[n];
int main(int argc, char* argv[])
{
int i;

printf("-----------\n1.注册\n2.登陆\n3.继续\n0.退出\n");
scanf("%d",&i);
switch(i)
{case 0: break;
case 1 : zhuce();break;
case 2: denglu();break;

}

return 0;
}
void zhuce( )
{char temp1[n],temp2[n],temp3[n],yhmtmp[n];

printf("输入用户名\n");
fflush(stdin);//清空缓存
gets(yhmtmp);

printf("输入密码\n");
fflush(stdin);
gets(temp1);
printf("输入密码确认\n");
fflush(stdin);
gets(temp2);
if(!strcmp(temp1,temp2))
{strcpy(mm,temp1);
printf("注册成功\n");

}
else
{printf("输入密码确认\n");
gets(temp3);
if(!strcmp(temp1,temp3))
{strcpy(mm,temp1);
printf("注册成功\n");

}
else

printf("注册失败\n");

}

}
void denglu( )
{
char s1[n],s2[n];
printf("输入用户名\n");
fflush(stdin);
gets(s1);
printf("输入密码\n");
fflush(stdin);
gets(s2);
if((strcmp(s1,yhm))&&(strcmp(s2,mm)))
printf("登陆成功\n");

}
相似回答