C++实现登录注册(文件储存和读取)

C++实现登录注册,并将数据保存到文件(txt文档中)

实现要求:
1.如果不存在初始用户数据文件则创建用户数据文件
2.实现完整的注册登录
3.实现用户数据的自动保存和读取
4.密码输入提供直接显示和隐式显示
在这里插入图片描述

1.运行截图

运行结果如下:
在这里插入图片描述

2.实现代码

代码如下:

#include "iostream"
#include "fstream"
#include "string"
#include <conio.h>
using namespace std;

#define SIZE 100//最大用户容量
int scount = 0;//用作储存当前已注册用户数

//用户类
class User
{
private:
	string phone;//电话
	string password;//密码
public:
	User() {};
	void Registers();//注册
	void Login();//登录
	void save();//保存
	void read();//读取
}us;

User user[SIZE];

//保存
void User::save()
{
	ofstream ofile;
	ofile.open("user.txt", ios::out);

	for (int i = 0; i < scount; i++)
	{
		ofile << user[i].phone << endl;
		ofile << user[i].password << endl;
	}
	ofile.close();
}

//读取
void User::read()
{
	ifstream ifile;
	ifile.open("user.txt", ios::in);

	scount = 0;

	if (!ifile.is_open())
	{
		//cout << "文件打开失败!" << endl;
		return;
	}

	for (int i = 0; !ifile.eof(); i++)
	{
		ifile >> user[i].phone;
		ifile >> user[i].password;
		scount++;
	}
	scount--;
	ifile.close();
}

//注册
void User::Registers()
{
	us.read();//读取已储存用户数据
	string ph;//电话
	string pw1;//密码1
	string pw2;//密码2
	for (int i = scount; i < SIZE; i++)
	{
	here:
		cout << "\t\t\t【系统提示】请输入手机号:";
		cin >> ph;
		//判断新输入的用户信息是否已存在(如果已存在则重新输入)
		for (int i = 0; i < scount; i++)
		{
			if (ph == user[i].phone)
			{
				cout << "\t\t\t【系统提示】用户已存在!" << endl;
				goto here;
			}
		}
		user[i].phone = ph;

		int chose = -1;
		cout << endl;
		cout << "\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
		cout << "\t\t\t┃       1.显示密码     2.隐藏密码      ┃\t\n";
		cout << "\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
		cout << "\t\t\t【系统提示】请输入你的选择:";
		cin >> chose;
		if (chose > 2 || chose < -1)
		{
			cout << "\t\t\t【系统提示】输入格式错误,请重新输入:";
			cin >> chose;
		}

		string pword;
		char ch, passwords0[20];
		int x = 0;
		string pword1;
		char ch1, passwords1[20];
		int x1 = 0;
		switch (chose)
		{
		case 1:
			cout << "\t\t\t【系统提示】请输入密码:";
			cin >> pword;
			user[i].password = pword;
			cout << "\t\t\t【系统提示】请再次密码:";
			cin >> pword1;
			if (pword1 != user[i].password)
			{
				cout << "\t\t\t【系统提示】密码不一致!" << endl;
				goto here;
			}
			else
			{
				scount++;//已注册用户加1
				cout << "\t\t\t【系统提示】注册成功!" << endl;
				us.save();//保存用户数据
			}
			break;
		case 2:
			cout << "\t\t\t【系统提示】请输入密码:";
			while ((ch = _getch()) != '\r' && x <= 20)
			{
				if (ch == '\b')
				{
					if (x > 0)
					{
						x--;
						cout << "\b \b";//密码支持退格的实现
					}
					else
						putchar(7);
				}
				else
				{
					passwords0[x++] = ch;
					printf("*");
				}
			}
			passwords0[x] = '\0';
			cout << endl;
			user[i].password = passwords0;

			cout << "\t\t\t【系统提示】请再次密码:";
			while ((ch1 = _getch()) != '\r' && x1 <= 20)
			{
				if (ch1 == '\b')
				{
					if (x1 > 0)
					{
						x1--;
						cout << "\b \b";//密码支持退格的实现
					}
					else
						putchar(7);
				}
				else
				{
					passwords1[x1++] = ch1;
					printf("*");
				}
			}
			passwords1[x1] = '\0';
			cout << endl;
			//比较两次输入的密码是否统一(如果不统一则重新输入)
			if (passwords1 != user[i].password)
			{
				cout << "\t\t\t【系统提示】密码不一致!" << endl;
				goto here;
			}
			else
			{
				scount++;//已注册用户加1
				cout << "\t\t\t【系统提示】注册成功!" << endl;
				us.save();//保存用户数据
			}
			break;
		}
		char choice;
		cout << "\t\t\t【系统提示】是否继续注册(Y/N)? :";
		while (1)
		{
			cin >> choice;
			if (choice == 'y' || choice == 'Y' || choice == 'n' || choice == 'N')
				break;
			else
				cout << "\t\t\t【系统提示】输入格式错误,请重新输入: ";
		}
		if (choice == 'n' || choice == 'N')
		{
			break;
		}
	}
}

//登录
void User::Login()
{
	us.read();//读取已储存用户数据
	string ph;//电话
	string pw;//密码
	int time = 0;//统计比较次数
here:
	cout << "\t\t\t【系统提示】请输入手机号:";
	cin >> ph;
	int chose = -1;
	cout << endl;
	cout << "\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
	cout << "\t\t\t┃       1.显示密码     2.隐藏密码      ┃\t\n";
	cout << "\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
	cout << "\t\t\t【系统提示】请输入你的选择:";
	cin >> chose;
	if (chose > 2 || chose < -1)
	{
		cout << "\t\t\t【系统提示】输入格式错误,请重新输入:";
		cin >> chose;
	}

	string pword;
	char ch, passwords0[20];
	int x = 0;
	switch (chose)
	{
	case 1:
		cout << "\t\t\t【系统提示】请输入密码:";
		cin >> pword;
		for (int i = 0; i < scount; i++)
		{
			if (ph == user[i].phone && pword == user[i].password)
			{
				time++;
				//cout << "\t\t\t【系统提示】登录成功!" << endl;
			}
		}
		if (time == 0)
		{
			cout << "\t\t\t【系统提示】手机号或密码错误!" << endl;
			goto here;
		}
		break;
	case 2:
		cout << "\t\t\t【系统提示】请输入密码:";
		while ((ch = _getch()) != '\r' && x <= 20)
		{
			if (ch == '\b')
			{
				if (x > 0)
				{
					x--;
					cout << "\b \b";//密码支持退格的实现
				}
				else
					putchar(7);
			}
			else
			{
				passwords0[x++] = ch;
				printf("*");
			}
		}
		passwords0[x] = '\0';
		cout << endl;
		//依次比较已储存信息,比较是否匹配(如不匹配则提示错误)
		for (int i = 0; i < scount; i++)
		{
			if (ph == user[i].phone && passwords0 == user[i].password)
			{
				time++;
				cout << "\t\t\t【系统提示】登录成功!" << endl;
			}
		}
		if (time == 0)
		{
			cout << "\t\t\t【系统提示】手机号或密码错误!" << endl;
			goto here;
		}
		break;
	}
}

int main()
{
	User user;
	int chose = -1;
	cout << endl;
	cout << "\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
	cout << "\t\t\t┃       1.注    册     2.登    录      ┃\t\n";
	cout << "\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
	cout << "\t\t\t【系统提示】请输入你的选择:";
	cin >> chose;
	if (chose > 2 || chose < -1)
	{
		cout << "\t\t\t【系统提示】输入格式错误,请重新输入:";
		cin >> chose;
	}
	switch (chose)
	{
	case 1:user.Registers(); break;
	case 2:user.Login(); break;
	}
}

代码小白,仅作学习记录📝

  • 59
    点赞
  • 362
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是一个简单的 C++ 程序实现注册和登录功能,使用文件来保存用户信息。为了简化程序,这里没有实现密码加密等安全性相关的功能,仅供参考: ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; struct User { string username; string password; }; vector<User> users; // 读取用户信息 void read_users() { ifstream fin("users.txt"); string username, password; while (fin >> username >> password) { users.push_back({username, password}); } fin.close(); } // 写入用户信息 void write_users() { ofstream fout("users.txt"); for (auto user : users) { fout << user.username << " " << user.password << endl; } fout.close(); } // 注册 void register_user() { User user; cout << "请输入用户名: "; cin >> user.username; cout << "请输入密码: "; cin >> user.password; users.push_back(user); write_users(); cout << "注册成功!" << endl; } // 登录 void login() { string username, password; cout << "请输入用户名: "; cin >> username; cout << "请输入密码: "; cin >> password; for (auto user : users) { if (user.username == username && user.password == password) { cout << "登录成功!" << endl; return; } } cout << "用户名或密码错误!" << endl; } int main() { read_users(); int choice; while (true) { cout << "请选择操作:" << endl; cout << "1. 注册" << endl; cout << "2. 登录" << endl; cout << "3. 退出" << endl; cin >> choice; switch (choice) { case 1: register_user(); break; case 2: login(); break; case 3: return 0; default: cout << "无效的选项!" << endl; } } return 0; } ``` 在上述程序中,用户信息保存在一个 users 的 vector 中,通过文件读写来实现注册和登录功能。注册时,程序会提示用户输入用户名和密码,然后将信息保存到 users 中,并写入到文件中。登录时,程序会提示用户输入用户名和密码,然后在 users 中查找是否存在匹配的用户,如果存在,则登录成功;否则,提示用户名或密码错误。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值