c++登录注册功能实现代码
// ConsoleApplication26.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <iostream>
#include<fstream>
#include<conio.h>
#include "ConsoleApplication26.h"
using namespace std;
struct user
{
string password;
string name;
};
void home_interface() {
cout << "hello home" << endl;
}
void regist()
{ //注册
char* password = new char[20];//分配20个字节,然后返回首地址给password,即password可以看做一个20字节的字符数组,即字符串
string name;
/*struct user
{
string password;
string name;
};定义的用户结构体*/
user c;
ifstream fin;//声明 ifstream 对象
fin.open("user.txr", ios::in);//关联文件
cout << "请输入用户名:";
cin >> name;
while (fin >> c.name >> c.password)//读取成功返回1
{
if (c.name == name)
{
cout << "该用户名已被注册" << endl; regist();
}
}
char test; int i = 0;
cout << "输入密码:";
while ((test = getch()) != '\r')
{
if (test == 8)//ASC2码8为退格
{
if (i > 0)
{
cout << test << " " << test;
password[i--] = '\0';
}
}
else
{
if (i < 20)
{
cout << "*";
password[i] = test;
i++;
}
}
}
password[i] = '\0';
cout << "再次输入密码";
char* password2 = new char[20];
i = 0;
while ((test = getch()) != '\r')
{
if (test == 8)
{
if (i > 0)
{
cout << test << " " << test;
password2[i--] = '\0';
}
}
else
{
if (i < 20)
{
cout << "*";
password2[i] = test;
i++;
}
}
password2[i] = '\0';
}
if (strcmp(password, password2) != 0)
{
cout << "密码两次输入不正确";
regist();
}
else
{
cout << "注册成功";
}
ofstream fout;
fout.open("user.txt", ios_base::out | ios_base::app);
fout << name << " " << password << endl;
fout.close();//关闭文件连接,但不会删除流
cout << "三秒后返回登陆界面";
home_interface();//界面
}
void login()
{//登录
char* password = new char[20];
string name;
cout << "用户名:" << endl;
cin >> name;
cout << " 密码:" << endl;
char test; int i = 0;
while ((test = getch()) != '\r')
{
if (test == 8)//ASC2码8为退格
{
if (i > 0)
{
cout << test << " " << test;
password[i--] = '\0';
}
}
else
{
if (i < 20)
cout << "*";
password[i] = test;
i++;
}
}
password[i] = '\0';
ifstream fin;
fin.open("user.txr", ios_base::in);
if (fin.fail())
{
cout << "文件打开失败";
}
user c; int f1;
while (fin >> c.name >> c.password)
{
if (c.name == name && c.password == password)
{
cout << "登陆成功";
f1 = 1;//登录成功标志
home_interface();
}
}
if (f1 == 0)//f1==0登录失败
{
cout << "用户名或密码错误";
login();
}
}
int main()
{
login();
}
posted on 2023-02-11 15:34 shenhshihao 阅读(565) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
2022-02-11 ubuntu搭建wordpress服务器+history学习