代码如下:
#include<stdio.h>
#pragma warning(disable:4996)
#include<string.h>
int main()
{
int i = 0
char password[10] = { 0 }
while (i <3)
{
scanf("%s", password)
printf("\n")
if (strcmp(password, "972816") == 0)
{
printf("登录成功\n")
break
}
else
{
i++
if (i != 3)
printf("陪兄再输入一次")
}
}
if (i == 3)
printf("密码错误三次退出登录界面\n")
system("pause")
return 0
扩展资料:
#include后面有两种方式,<>;和""前者先在标准库中查找,查找不到在path中查找。后者为文件路径,若直接是文件名则在项目根目录下查找。
引用方法:#include <stdio.h>
注意事项:在TC2.0中,允许不引用此头文件而直接调用其宽乱旦中的函数,但这种做法是不标准的。也不建议这样做。以避慎扰免出现在其他IDE中无法编译或执行的问题。
参考资料来源:百度百科—include
参考资料来源:百度百科—stdio.h
#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=0i<totali++)
{
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=0i<totali++)
{
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
}
#include <stdlib.h>#include <stdio.h>
#include <string.h>
bool search(char id[], char pass[]) {
FILE *fp
char tid[10], tpass[10]
fp = fopen("c:\\data", "r")
while (!feof(fp)) {
fscanf(fp, "%s%s", tid, tpass)
if (strcmp(tid, id)==0 &&strcmp(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[]) {
FILE *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 (strcmp(pass, tpass) != 0)
printf("The passwords you pressed are not the same!\n")
else
break
}
_add(id, pass)
printf("-----------------------Register successfully!\n")
}
void init() {
FILE *fp
if ((fp=fopen("c:\\data", "r")) == NULL) { // 注意,一定要有个名叫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
}
搞定了。。。我是用成功了的。。。如果有问题就发信给我。。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)