我是新手,刚接触到C++语言,这是我用C++语言写 用户帐号登录功能,可以把账号密码数据本地保存 然后在获取账号密码数据登录的小程序,程序中有些小bug,希望得到大神指点~
#include <iostream>
#include <windows.h>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
using namespace std;
#define C 60
#define L 20
typedef struct _users
{
char id[17];
char passwds[17];
}MANuser;
MANuser hu;
void welcomeUI();//欢迎界面
void Setcursors(int x,int y);//设置光标
void HideCursor();
void PlayUsers();//用户注册 登录界面
void RemoveScreen();//清屏
void UserPassword(MANuser& mans);//注册
void Login(MANuser& mans);//登录
void FileLocal(MANuser& mans);//数据存本地
MANuser FileRead();//数据读取
声明函数
//欢迎界面
void welcomeUI()
{
//Setcursors(1,0);
for(int i = 1;i < L - 1; ++i)
{
Setcursors(1,0 + i);
for(int j = 1; j < C - 1; ++j)
{
if(i == 1 || i == L - 2)
{
cout << "-";
}
else if(i == 3)
{
cout << "-";
}
if(i != 1 && ( j == 1 || j == (C-2)))
{
Setcursors(0 + j,0 + i);
cout << "|";
}
}
}
}
//设置光标
void Setcursors(int x,int y)
{
HANDLE handle;
COORD coord = {x,y};
handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle,coord);
}
//隐藏光标
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//用户注册 登录界面
void PlayUsers()
{
Setcursors(18,2);
cout << "欢迎来到********小游戏!!";
Setcursors(15,4);
cout << "a、d键左右选择,k键确认,0退出";
char key;
int a = 1;
Setcursors(13,15);
cout << "1.新用户" << "\t\t \t" << "2.老玩家";
Setcursors(10,15);
cout << ">>";
Setcursors(60,30);
cout << " ";
while(a)
{
key = getch();
switch(key)
{
case 'a':
Setcursors(10,15);
cout << ">>";
Setcursors(37,15);
cout << " ";
Setcursors(60,30);
cout << " ";
a=1;
break;
case 'd':
Setcursors(10,15);
cout << " ";
Setcursors(37,15);
cout << ">>";
Setcursors(60,30);
cout << " ";
a=2;
break;
case 'k':
if(a == 1) //注册
{
RemoveScreen();
UserPassword(hu);
Sleep(800);
RemoveScreen();
PlayUsers();
}else if(a == 2) //登录
{
Setcursors(25,30);
cout << hu.id << endl;
cout << hu.passwds << endl;
RemoveScreen();
MANuser ming = FileRead();
Login(ming);
}
a = 0;
break;
case '0':
Setcursors(60,30);
cout << " ";
exit(0);
Setcursors(60,30);
cout << " ";
break;
}
}
}
//清屏
void RemoveScreen()
{
for(int i = 5;i < L - 2; ++i)
{
Setcursors(2,0 + i);
for(int j = 2; j < C - 2; ++j)
{
cout << " ";
}
}
}
//注册
void UserPassword(MANuser& mans)
{
int a = 1;
Setcursors(15,4);
cout << " ";
Setcursors(15,5);
cout << "用户名和密码不能超过 16 位字符";
Setcursors(20,17);
cout << "输入完成按回车键";
char Users[17];
char Users_two[17];
char Passwords[17];
char Passwords2[17];
Setcursors(12,8);
cout << "(帐号) 用户名:";
Setcursors(12,10);
cout << "(第一次)密 码:";
Setcursors(12,12);
cout << "(第二次)密 码:";
Setcursors(8,8);
cout << ">>";
Setcursors(28,8);
cin >> Users;
strcpy(Users_two,Users);
do
{
if(strlen(Users) >= 17)
{
Setcursors(28,8);
cout << "用户名过长,请重新输入 ";
Sleep(1000);
Setcursors(28,8);
cout << " ";
Setcursors(28,8);
cin >> Users;
strcpy(Users_two,Users);
}
else break;
}while(a);
Setcursors(8,8);
cout << " ";
Setcursors(8,10);
cout << ">>";
Setcursors(28,10);
cin >> Passwords;
do
{
if(strlen(Passwords) >= 17)
{
strcpy(Passwords,"0");
Setcursors(28,10);
cout << "密码过长,请重新输入 ";
Sleep(1000);
Setcursors(28,10);
cout << " ";
Setcursors(28,10);
cin >> Passwords;
strcpy(Users,Users_two);
}
else break;
}while(a);
Setcursors(8,10);
cout << " ";
Setcursors(8,12);
cout << ">>";
Setcursors(28,12);
cin >> Passwords2;
do
{
if(strlen(Passwords2) >= 17)
{
strcpy(Passwords2,"0");
Setcursors(28,12);
cout << "密码过长,请重新输入 ";
Sleep(1000);
Setcursors(28,12);
cout << " ";
Setcursors(28,12);
cin >> Passwords2;
strcpy(Users,Users_two);
}
else break;
}while(a);
if(strcmp(Passwords,Passwords2) != 0)
{
RemoveScreen();
Setcursors(22,6);
cout << "密码输入不一致";
Setcursors(12,8);
cout << "(帐号) 用户名:";
Setcursors(28,8);
cout << " ";
strcpy(Users,Users_two);
Setcursors(28,8);
cout << Users;
Setcursors(12,10);
cout << "(第一次)密 码:";
Setcursors(12,12);
cout << "(第二次)密 码:";
while(1)
{
Setcursors(28,10);
cout << " ";
Setcursors(28,12);
cout << " ";
Setcursors(8,12);
cout << " ";
Setcursors(8,10);
cout << ">>";
Setcursors(28,10);
cin >> Passwords;
do
{
if(strlen(Passwords) >= 17)
{
strcpy(Passwords,"0");
Setcursors(28,10);
cout << "密码过长,请重新输入 ";
Sleep(1000);
Setcursors(28,10);
cout << " ";
Setcursors(28,10);
cin >> Passwords;
strcpy(Users,Users_two);
}
else break;
}while(a);
Setcursors(8,10);
cout << " ";
Setcursors(8,12);
cout << ">>";
Setcursors(28,12);
cin >> Passwords2;
do
{
if(strlen(Passwords2) >= 17)
{
strcpy(Passwords2,"0");
Setcursors(28,12);
cout << "密码过长,请重新输入 ";
Sleep(1000);
Setcursors(28,12);
cout << " ";
Setcursors(28,12);
cin >> Passwords2;
strcpy(Users,Users_two);
}
else break;
}while(a);
if(strcmp(Passwords,Passwords2) == 0)
{
break;
}
}
}
RemoveScreen();
Setcursors(25,12);
cout << "注册成功";
strcpy(mans.id,Users);
strcpy(mans.passwds,Passwords2);
FileLocal(hu);
}
//登录
void Login(MANuser& mans)
{
int num = 3;
Setcursors(15,4);
cout << "勇猛的战士,笑傲江湖需要你的拯救";
Setcursors(20,17);
cout << "输入完成按回车键";
char Users[17];
char Passwords[17];
Setcursors(8,8);
cout << ">>";
Setcursors(12,8);
cout << " (帐号)用户名:";
Setcursors(12,12);
cout << " 密 码:";
Setcursors(28,8);
cin >> Users;
Setcursors(8,8);
cout << " ";
Setcursors(8,12);
cout << ">>";
Setcursors(28,12);
cin >> Passwords;
if( (strcmp(mans.id,Users) != 0) || (strcmp(mans.passwds,Passwords) != 0) )
{
RemoveScreen();
Setcursors(15,11);
cout << "您输入的ID或密码有误,请重新输入";
Sleep(1000);
Setcursors(12,8);
cout << "(帐号) 用户名:";
Setcursors(28,8);
cout << " ";
Setcursors(12,12);
cout << " 密 码:";
Setcursors(28,12);
cout << " ";
--num;
while(1)
{
if(num <=0)
{
RemoveScreen();
PlayUsers();
num = 3;
break;
}
Setcursors(12,8);
cout << "(帐号) 用户名:";
Setcursors(28,8);
cout << " ";
Setcursors(12,12);
cout << " 密 码:";
Setcursors(28,12);
cout << " ";
Setcursors(15,11);
cout << "您输入的ID或密码有误,请重新输入";
Sleep(10);
Setcursors(15,11);
cout << " ";
Setcursors(22,6);
cout << "你还有 " << num << " 次机会";
Setcursors(8,12);
cout << " ";
Setcursors(8,8);
cout << ">>";
Setcursors(28,8);
cin >> Users;
Setcursors(8,8);
cout << " ";
Setcursors(8,12);
cout << ">>";
Setcursors(28,12);
cin >> Passwords;
if( (strcmp(mans.id,Users) == 0) && (strcmp(mans.passwds,Passwords) == 0) )
{
break;
}
--num;
}
}
RemoveScreen();
Setcursors(20,12);
cout << "欢迎来到笑傲江湖世界" << endl;
Sleep(5000);
RemoveScreen();
PlayUsers();
}
//数据存本地
void FileLocal(MANuser& mans)
{
char *Path = "C://Users//Administrator//Desktop//Data.txt";
FILE *fp = fopen(Path,"wb");
if(fp == NULL)
{
cout << "文件未生成" << endl;
}
MANuser *file = (MANuser *)malloc(sizeof(MANuser));
strcpy(file->id,mans.id);
strcpy(file->passwds,mans.passwds);
//fprintf(fp,"id=%s,password=%s\n",file->id,file->passwds);
fwrite(file->id,1,sizeof(file->id),fp);
fwrite(file->passwds,1,sizeof(file->passwds),fp);
free(file);
fclose(fp);
}
//数据读取
MANuser FileRead()
{
char *Path = "C://Users//Administrator//Desktop//Data.txt";
FILE *fp = fopen(Path,"rb");
if(fp == NULL)
{
cout << "文件未生成" << endl;
}
MANuser who;
fread(who.id,1,sizeof(who.id),fp);
fread(who.passwds,1,sizeof(who.passwds),fp);
fclose(fp);
return who;
}
主函数
int main()
{
SetConsoleTitle("用户登录");//控制台标题
system("mode con cols=60 lines=40");//控制窗口size
Setcursors(18,2);
cout << "欢迎来到********小游戏!!";
Setcursors(15,4);
cout << "a、d键左右选择,k键确认,0退出";
HideCursor();
system("color 0a");//字体颜色
welcomeUI();
RemoveScreen();
PlayUsers();
Setcursors(0,30);
return 0;
}