栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++账号注册登录

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++账号注册登录

#include
#include
#include
#include                 

using namespace std;

#define FILENAME "Account.txt"
#define SIZE 100              //最大容量
map arr_user;

int Check_If_Int(string);
void ShowMenu();

class User {
public:

	//注册账号
	void Register(User *user) {
		//读取已注册账号记录
		user->Read();

		int i_acc;
		int i_psw;
		int i_psw02; //用于校验两次密码是否一致

		string str_acc;
		string str_psw;
		string str_psw02;

		for (int i = arr_user.size(); i < SIZE; i++) {
		FLAG01:

			cout << "请输入账号:";

			cin >> str_acc;

			//检查是否存在特殊字符
			int ret01 = Check_If_Int(str_acc);
			if (ret01 != 0)
				goto FLAG01;

			i_acc = atoi(str_acc.c_str());

			//查找是否已注册
			auto pos_acc = arr_user.find(i_acc);
			if (pos_acc != arr_user.end()) {
				cout << "已存在该账号,请返回登陆" << endl;
				system("pause");
				system("cls");
				return;
			}

		FLAG02:
			cout << "请输入密码:";
			cin >> str_psw;
			
			//检查是否存在特殊字符
			int ret02 = Check_If_Int(str_psw);
			if (ret02 != 0)
				goto FLAG02;

			i_psw = atoi(str_psw.c_str());

			//注册密码 二次确认
			cout << "请再次输入密码:";
			cin >> str_psw02;

			//检查是否存在特殊字符
			int ret03 = Check_If_Int(str_psw02);
			if (ret03 != 0)
				goto FLAG02;

			i_psw02 = atoi(str_psw02.c_str());



			if (i_psw != i_psw02) {
				cout << "两次密码不一致,请重新输入" << endl;
				system("pause");
				system("cls");
				goto FLAG02;
			}

			//插入map
			arr_user.insert(make_pair(i_acc, i_psw));

			//保存文件
			user->Save();

			cout << "注册成功,请返回登陆" << endl;
			system("pause");
			system("cls");
			break;

		}

	}

	//读取数据
	void Read() {
		ifstream fin;
		fin.open(FILENAME, ios::in);

		if (!fin.is_open()) {
			//cout<<"记录为空";
			return;
		}

		int i_acc;
		int i_psw;
		auto it = arr_user.begin();

		//存入map
		while (!fin.eof()) {
			fin >> i_acc;
			fin >> i_psw;
			arr_user.insert(make_pair(i_acc, i_psw));

		}

		fin.close();

	}

	//存储数据
	void Save() {
		ofstream fout;
		fout.open(FILENAME, ios::out);

		for (auto it = arr_user.begin(); it != arr_user.end(); it++) {
			fout << it->first << " " << it->second << endl;
		}

		fout.close();
	}

	//登陆账号
	void Login(User* user) {

		user->Read();

		//判断是否已有注册账号
		ifstream fin;
		char ch;
		fin.open(FILENAME, ios::in);
		fin >> ch;
		if (fin.eof()) {
			//cout << "文件为空" << endl;
			cout << "无账号记录" << endl;
			system("cls");
			return;
		}

		int i_acc;
		int i_psw;
		string str_acc;
		string str_psw;

		//输入账号
	FLAG03:
		cout << "请输入账号:";
		cin >> str_acc;

		//检查是否存在特殊字符
		int ret = Check_If_Int(str_acc);
		if (ret != 0)
			goto FLAG03;

		i_acc = atoi(str_acc.c_str());

		//查找是否存在该账号
		auto pos_acc = arr_user.find(i_acc);
		if (pos_acc == arr_user.end()) {
			cout << "该账号未注册" << endl;
			goto FLAG03;
		}

		//输入密码
	FLAG04:
		cout << "请输入密码:";
		cin >> str_psw;

		//检查是否存在特殊字符
		int ret02 = Check_If_Int(str_psw);
		if (ret02 != 0)
			goto FLAG04;

		i_psw = atoi(str_psw.c_str());

		//匹配账号密码
		if (pos_acc->second == i_psw) {
			cout << "登陆成功" << endl;
		}
		else {
			cout << "密码错误,请重新登录" << endl;
			system("pause");
			system("cls");
			goto FLAG03;
		}

		system("pause");
		system("cls");
	}

	//修改密码
	void ChangePassword(User* user) {
		user->Read();

		//判断是否已有注册账号
		ifstream fin;
		char ch;
		fin.open(FILENAME, ios::in);
		fin >> ch;
		if (fin.eof()) {
			//cout << "文件为空" << endl;
			cout << "无账号记录" << endl;
			system("pause");
			system("cls");
			return;
		}

		string str_acc;
		string str_psw_old; //旧密码
		string str_psw_new; //新密码
		int i_acc;
		int i_psw_old;
		int i_psw_new;

	FLAG01:
		cout << "请输入修改账户:";
		cin >> str_acc;

		//检查是否存在特殊字符
		int ret = Check_If_Int(str_acc);
		if (ret != 0)
			goto FLAG01;

		i_acc = atoi(str_acc.c_str());

		//查找是否存在该账号
		auto pos_acc = arr_user.find(i_acc);
		if (pos_acc == arr_user.end()) {
			cout << "该账号未注册" << endl;
			system("pause");
			system("cls");
			goto FLAG01;
		}

	FLAG02:
		cout << "请输入原密码:";
		cin >> str_psw_old;

		//检查是否存在特殊字符
		int ret02 = Check_If_Int(str_psw_old);
		if (ret02 != 0)
			goto FLAG02;

		i_psw_old = atoi(str_psw_old.c_str());

		if (pos_acc->second != i_psw_old) {
			cout << "密码错误,请重试" << endl;
			system("pause");
			system("cls");
			goto FLAG01;
		}

	FLAG03:
		cout << "请输入新密码:";
		cin >> str_psw_new;

		//检查是否存在特殊字符
		int ret03 = Check_If_Int(str_psw_new);
		if (ret03 != 0)
			goto FLAG03;

		i_psw_new = atoi(str_psw_new.c_str());

		pos_acc->second = i_psw_new;

		cout << "修改成功,正在返回" << endl;
		//保存文件
		user->Save();

		system("pause");
		system("cls");
	}

	//注销账号
	void Logout(User* user) {
		user->Read();

		//判断是否已有注册账号
		ifstream fin;
		char ch;
		fin.open(FILENAME, ios::in);
		fin >> ch;
		if (fin.eof()) {
			//cout << "文件为空" << endl;
			cout << "无账号记录" << endl;
			system("pause");
			system("cls");
			return;
		}

		string str_acc;
		string str_psw;
		int i_acc;
		int i_psw;

	FLAG01:
		cout << "请输入账号:";
		cin >> str_acc;

		//检查是否存在特殊字符
		int ret = Check_If_Int(str_acc);
		if (ret != 0)
			goto FLAG01;

		i_acc = atoi(str_acc.c_str());

		//查找是否存在该账号
		auto pos_acc = arr_user.find(i_acc);
		if (pos_acc == arr_user.end()) {
			cout << "该账号未注册" << endl;
			system("pause");
			system("cls");
			goto FLAG01;
		}

	FLAG02:
		cout << "请输入密码:";
		cin >> str_psw;

		//检查是否存在特殊字符
		int ret02 = Check_If_Int(str_acc);
		if (ret02 != 0)
			goto FLAG02;

		i_psw = atoi(str_psw.c_str());

		if (pos_acc->second != i_psw) {
			cout << "密码错误,请重试" << endl;
			system("pause");
			system("cls");
			goto FLAG02;
		}

	FLAG03:
		cout << "是否注销账户?" << endl;
		cout << "1.是             2.否" << endl;
		int choice;
		cin >> choice;

		if (choice == 1) {
			arr_user.erase(pos_acc);
			cout << "注销成功" << endl;
			user->Save();
		}
		else if (choice == 2) {
			return;
		}
		else {
			cout << "输入有误,请重试" << endl;
			system("pause");
			system("cls");
			goto FLAG03;
		}

		system("pause");
		system("cls");
	}

};

//检查是否字符为整型
int Check_If_Int(string check_ASCII) {

	for (int i = 0; i < check_ASCII.size(); i++) {

		if (check_ASCII[i] >= 48 && check_ASCII[i] <= 57) {
			continue;
		}
		else {
			cout << "使用非法字符,请重试" << endl;
			system("pause");
			system("cls");
			return -1;
			//break;

		}

	}

	return 0;

}

//显示菜单
void ShowMenu() {
	cout << "欢迎使用账号管理系统" << endl;
	cout << "1.注册" << endl;
	cout << "2.登录" << endl;
	cout << "3.修改密码" << endl;
	cout << "4.注销账号" << endl;
	cout << "0.退出" << endl;
}


int main() {

	User user;

	int choice = 0;

	while (true) {
		//菜单调用
		ShowMenu();

		cin >> choice;

		switch (choice) {
		case 1://注册
			user.Register(&user);
			break;
		case 2://登录
			user.Login(&user);
			break;
		case 3://修改密码
			user.ChangePassword(&user);
			break;
		case 4://注销账号
			user.Logout(&user);
			break;
		case 0://退出
			exit(0);
			break;
		default:
			cout << "输入有误,请重新输入" << endl;
			system("pause");
			system("cls");
			break;
		}

	}

	system("pause");
	return 0;
}

练手小程序

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号