C++:登录注册功能

codeview
在源代码的基础上进行了修改:

#include <fstream>
#include <iostream>
#include <limits>
#include <string>

template <typename T>
T get_input(const std::string &strQuery);

std::string get_username();
std::string get_password();
void save_user(const std::string &username, const std::string &password);

void login();
void register_user();
void main_menu();

int main()
{
	main_menu();
}

template <typename T>
T get_input(const std::string &strQuery)
{
	std::cout << strQuery << "\n> ";
	T out = T();

	while (!(std::cin >> out)) {
		std::cin.clear();
		std::cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n');
		std::cout << "Error!" "\n";
		std::cout << strQuery << "\n> ";
	}

	return out;
}

std::string get_password()
{
	std::string password1 = get_input <std::string>("Please enter your password.");
	std::string password2 = get_input <std::string>("Please re-enter your password.");

	while (password1 != password2) {
		std::cout << "Error! Passwords do not match." "\n";
		password1 = get_input <std::string>("Please enter your password.");
		password2 = get_input <std::string>("Please re-enter your password.");
	}

	return password1;
}

std::string get_username()
{
	std::string username = get_input <std::string>("Please enter a username.");
	std::cout << "Username: \"" << username << "\"\n";

	std::string filename = "accounts\\" + username + ".txt";
	if (FILE *f = fopen(filename.c_str(), "r"))
	{
		fclose(f);
		std::cout << "User exists!" << std::endl;
		register_user();
	}
	else 
	{
		while (get_input <int>("Confirm? [0|1]") != 1) {
				username = get_input <std::string>("Please enter a username.");
				std::cout << "Username: \"" << username << "\"\n";
			}
	}
	return username;
}

void login()
{
	std::string username = get_input <std::string>("Please enter your username.");
	std::cout << "Username: \"" << username << "\"\n";

	std::string filename = "accounts\\" + username + ".txt";
	if (FILE *f = fopen(filename.c_str(), "r"))
	{
		std::string psw = get_input <std::string>("Please enter your password.");
		std::ifstream readFile(filename);
		std::string p;
		readFile >> p;
		fclose(f);
		if (psw == p)
		{
			std::cout << "Login successed" << std::endl;
			main_menu();
		}
		else
		{
			std::cout << "Login failed" << std::endl;
			login();
		}
	}
	else
	{
		std::cout << "No User!" << std::endl;
		login();
	}

}

void main_menu()
{
	int choice = get_input <int>(
		"Hello, Would you like to log in or register?" "\n"
		"[1] Login" "\n"
		"[2] Register" "\n"
		"[3] Exit");

	switch (choice)
	{
	case 1:
		login();
		break;
	case 2:
		register_user();
		break;
	}
}

void register_user()
{
	std::string username = get_username();
	std::string password = get_password();
	save_user(username, password);
}

void save_user(const std::string &username, const std::string &password)
{
	std::string filename = "accounts\\" + username + ".txt";
	std::ofstream writefile(filename);
	writefile << password << "\n";
	main_menu();
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值