C++账号注册、登录、修改、注销

  • Post author:
  • Post category:其他


User.h

#pragma once
#include <iostream>
#include <string>
#include <map>
#include <fstream>

#define FILENAME "Account.txt"

using namespace std;


class User
{
public:

	ifstream fin;
	ofstream fout;

	User() = default;

	bool checkIfNum(string str);

	void read();

	void save();

	void registration();

	void login();

	void modify();

	void logout();

private:

	static map<string, string> mapUser;

	string m_acc;
	string m_psw;
	
};

User.cpp

#include "User.h"

map<string, string> User::mapUser;

bool User::checkIfNum(string str)
{
	for (auto c: str)
	{
		if (c >= 48 && c <= 57)
			continue;
		else
		{
			cout << "Please enter number" << endl;
			return false;
		}
	}
	return true;
}

void User::read()
{
	fin.open(FILENAME, ios::in);
	
	if (!fin.is_open()) 
	{
		cout << "No accounts" << endl;
		return;
	}

	while (!fin.eof())
	{
		fin >> m_acc >> m_psw;
		mapUser.insert(make_pair(m_acc, m_psw));
	}

	fin.close();
}

void User::save()
{
	fout.open(FILENAME, ios::out);

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

	fout.close();
}

void User::registration()
{
	this->read();

	string pswForCheck;

	while (true)
	{
		cout << "Please enter the account :";
		cin >> m_acc;
		if (!checkIfNum(m_acc)) 
			continue;
		
		auto pos_acc = mapUser.find(m_acc);
		if (pos_acc != mapUser.end()) 
		{
			cout << "This account has already been registered" << endl;
			cout << "Please go back and login" << endl;
			system("pause");
			return;
		}

		while (true)
		{
			cout << "Please enter the password :";
			cin >> m_psw;

			if (!checkIfNum(m_psw))
				continue;
			
			else
			{
				break;
			}
		}

		while (true)
		{
			cout << "Please enter the password again :";
			cin >> pswForCheck;

			if (!checkIfNum(pswForCheck))
				continue;
			
			if (m_psw != pswForCheck)
			{
				cout << "The passwords are inconsistent twice." << endl;
				continue;
			}
			else
			{
				break;
			}
		}
		
		cout << "The account has been registered successfully." << endl;
		 
		mapUser.insert(make_pair(m_acc, m_psw));
		this->save();

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

		break;
	}
}

void User::login()
{
	this->read();

	while (true)
	{
		cout << "Please enter the account :";
		cin >> m_acc;

		if (!checkIfNum(m_acc))
			continue;

		auto pos_acc = mapUser.find(m_acc);
		if (pos_acc == mapUser.end())
		{
			cout << "This account has not been registered yet." << endl;
			cout << "Please go back and register." << endl;
			continue;
		}

		while (true)
		{
			cout << "Please enter the password :";
			cin >> m_psw;

			if (!checkIfNum(m_psw))
				continue;

			if (pos_acc->second != m_psw)
			{
				cout << "The account or the password may be wrong." << endl;
				cout << "Please try again." << endl;
				continue;
			}
			else
				break;
		}

		cout << "The account has already logged in successfully." << endl;

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

		break;
	}
}

void User::modify()
{
	this->read();

	string newPsw;

	while (true)
	{
		cout << "Please enter the account :";
		cin >> m_acc;

		if (!checkIfNum(m_acc))
			continue;

		auto pos_acc = mapUser.find(m_acc);
		if (pos_acc == mapUser.end())
		{
			cout << "This account has not been registered yet." << endl;
			cout << "Please go back and register." << endl;
			continue;
		}

		while (true)
		{
			cout << "Please enter the password :";
			cin >> m_psw;

			if (!checkIfNum(m_psw))
				continue;

			if (pos_acc->second != m_psw)
			{
				cout << "The account or the password may be wrong." << endl;
				cout << "Please try again." << endl;
				continue;
			}
			else
				break;
		}
		while (true)
		{
			cout << "Please enter the new password :";
			cin >> newPsw;

			if (!checkIfNum(newPsw))
				continue;

			pos_acc->second = newPsw;

			this->save();

			cout << "This account has already been modified successfully" << endl;
			cout << "Please go back and login" << endl;

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

			break;
		}
		break;
	}
}

void User::logout()
{
	this->read();

	while (true)
	{
		cout << "Please enter the account :";
		cin >> m_acc;

		if (!checkIfNum(m_acc))
			continue;

		auto pos_acc = mapUser.find(m_acc);
		if (pos_acc == mapUser.end())
		{
			cout << "This account has not been registered yet." << endl;
			cout << "Please go back and register." << endl;
			continue;
		}

		while (true)
		{
			cout << "Please enter the password :";
			cin >> m_psw;

			if (!checkIfNum(m_psw))
				continue;

			if (pos_acc->second != m_psw)
			{
				cout << "The account or the password may be wrong." << endl;
				cout << "Please try again." << endl;
				continue;
			}
			else
				break;
		}
		
		mapUser.erase(pos_acc);

		cout << "This account has been logout successfully." << endl;

		this->save();

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

		break;
	}

}

main.cpp

#include <iostream>
#include "User.h"


using namespace std;

void showMenu()
{
	cout << "1.register" << endl;
	cout << "2.login" << endl;
	cout << "3.modify" << endl;
	cout << "4.logout" << endl;
	cout << "0.exit" << endl;
}

int main()
{
	User user;

	int choice = 0;

	while (true) 
	{
		showMenu();

		cin >> choice;

		switch (choice) {
		case 1:
			user.registration();
			break;
		case 2:
			user.login();
			break;
		case 3:
			user.modify();
			break;
		case 4:
			user.logout();
			break;
		case 0:
			exit(0);
			break;
		default:
			cout << "Wrong input." << endl;
			system("pause");
			system("cls");
			break;
		}

	}
	return 0;
}

运行状况如下

1.注册

2.登录

3.修改

返回登陆,密码已由12345变为123456

4.注销

返回登录该账号已被注销,需要重新注册



版权声明:本文为m0_59740457原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。