主函数
#include<iostream>
#include<string>
#include<cstdlib>
#include "functions.h"
#include "account.h"
using namespace std;
int main()
{
//创建账号录结构体
Accountrecords ads;
//初始化账号录中账号个数
int select = 0;
string paword;
kel:cout << "请输入密码:" << endl;
cin >> paword;
while (true)
{
if (paword == "123456")
{
showMenu();//显示菜单
cin >> select;
switch (select)
{
case 1://1、添加账号
addaccount(&ads);//利用地址传递
break;
case 2://2、显示账号
showAccount(&ads);
break;
case 3://3、删除账号
deleteAccount(&ads);
break;
case 4://4、查找账号
findAccount(&ads);
break;
case 5:// 5、修改账号
modfiyAccount(&ads);
break;
case 6://6、清空账号
cleanAccount(&ads);
break;
case 0://0、退出账号录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
break;
}
}
else
{
cout << "密码错误" << endl;
system("pause");
goto kel;
}
}
return 0;
}
结构体
#pragma once
#include<string>
#define Max 1000
using namespace std;
struct Account
{
//平台名称
string m_Name;
//账号
string m_account;
//密码
string m_password;
};
struct Accountrecords
{
//保存账号的数组
struct Account accountArray[Max];
//当前记录的账号个数
int m_Size = 0;
};
函数的声明
#pragma once
#include "account.h"
void showMenu();//显示菜单
void addaccount(Accountrecords* ads);//case 1添加账号
void showAccount(Accountrecords* ads);//case2显示账号
void deleteAccount(Accountrecords* ads);//case3删除账号
void findAccount(Accountrecords* ads);//case 4查找账号
void modfiyAccount(Accountrecords* ads);//case 5修改账号
void cleanAccount(Accountrecords* ads);//case 6清空账号
函数的定义
#include <iostream>
#include "account.h"
#include <fstream>
using namespace std;
void showMenu()//显示菜单
{
cout << "****************************" << endl;
cout << "***** 1、添加账号 *****" << endl;
cout << "***** 2、显示账号 *****" << endl;
cout << "***** 3、删除账号 *****" << endl;
cout << "***** 4、查找账号 *****" << endl;
cout << "***** 5、修改账号 *****" << endl;
cout << "***** 6、清空账号 *****" << endl;
cout << "***** 0、退出账号录 *****" << endl;
cout << "****************************" << endl;
}
void addaccount(Accountrecords *ads)//case 1添加账号
{
//判断账号录是否已满,如果满了就不在添加
if (ads->m_Size == Max)
{
cout<<"账号录已满,无法添加" << endl;
return;
}
else
{
//添加具体账号
//平台名称
string name;
cout << "请输入账号名称:" << endl;
cin >> name;
ads->accountArray[ads->m_Size].m_Name = name;
//账号
string account;
cout << "请输入账号:" << endl;
cin >> account;
ads->accountArray[ads->m_Size].m_account = account;
//密码
string password;
cout << "请输入密码:" << endl;
cin >> password;
ads->accountArray[ads->m_Size].m_password = password;
//创建流对象
ofstream ofs;
//打开文件
ofs.open("account.txt", ios::out | ios::binary);
//写文件
Account* p = &ads->accountArray[ads->m_Size];
ofs.write((const char*)p, sizeof(Accountrecords));
//5-关闭文件
ofs.close();
}
//更新账号录人数
ads->m_Size++;
cout << "添加成功!" << endl;
system("pause");//按任意键继续、
system("cls");//清屏
}
void showAccount(Accountrecords *ads)//case2显示账号
{
//判断账号录中人数是否为0
if (ads->m_Size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
//1-包含头文件
//2-创建流对象
ifstream ifs;
//3-打开文件 判断文件是否打开成功
ifs.open("account.txt", ios::in | ios::binary);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//4-读文件
Account* p = &ads->accountArray[ads->m_Size];
ifs.read((char*)p, sizeof(Accountrecords));
for (int i = 0; i < ads->m_Size; i++)
{
cout << "平台名称:" << ads->accountArray[i].m_Name << '\t';
cout << "账号:" << ads->accountArray[i].m_account << '\t';
cout << "密码:" << ads->accountArray[i].m_password<< endl;
}
}
system("pause");//按任意键继续、
system("cls");//清屏
}
//检测账号是否存在,如果存在,返回账号所在数组中的具体位置,不存在,返回-1
int isExist(Accountrecords * ads, string name)
{
for (int i = 0; i < ads->m_Size; i++)
{//找到用户输入的平台名称
if (ads->accountArray[i].m_Name == name)
{
return i;//找到返回平台名称的下标编号
}
}
return -1;//如果遍历结束都没有找到,返回-1
}
void deleteAccount(Accountrecords* ads)//case3删除账号
{
cout << "请输入您要账号平台名称" << endl;
string name;
cin >> name;
int ret = isExist(ads, name);
if (ret != -1)
{
//查到此账号,删除
for (int i = ret; i < ads->m_Size; i++)
{
//数据前移
ads->accountArray[i] = ads->accountArray[i + 1];
ads->m_Size--;//更新账号录中的账号数
cout << "删除成功!" << endl;
}
}
else
{
cout << "没有此账号" << endl;
}
system("pause");
system("cls");
}
void findAccount(Accountrecords* ads)//case 4查找账号
{
cout << "请输入您要查找账号:" << endl;
string name;
cin >> name;
//判断指定的账号是否存在账号录中
int ret = isExist(ads, name);
if (ret != -1)//找到账号
{
cout << "平台名称:" << ads->accountArray[ads->m_Size].m_Name << '\t';
cout << "账号:" << ads->accountArray[ads->m_Size].m_account << '\t';
cout << "密码:" << ads->accountArray[ads->m_Size].m_password << endl;
}
else//未找到账号
{
cout << "查无此账号!" << endl;
}
system("pause");
system("cls");
}
void modfiyAccount(Accountrecords* ads)//case 5修改账号
{
string paword;
cout << "请输入系统密码:" << endl;
cin >> paword;
if (paword == "123456")
{
cout << "请输入您要修改的账号:" << endl;
string name;
cin >> name;
//判断指定的账号是否存在账号录中
int ret = isExist(ads, name);
if (ret != -1)//找到账号
{
//平台名称
string name;
cout << "请输入平台名称:" << endl;
cin >> name;
ads->accountArray[ads->m_Size].m_Name = name;
//账号
string account;
cout << "请输入账号:" << endl;
cin >> account;
ads->accountArray[ads->m_Size].m_account = account;
//密码
string password;
cout << "请输入密码:" << endl;
cin >> password;
ads->accountArray[ads->m_Size].m_password = password;
//更新账号录人数
ads->m_Size++;
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
}
else
{
cout << "密码错误" << endl;
return;
}
system("pause");
system("cls");
}
void cleanAccount(Accountrecords* ads)//case 6清空账号
{
string paword;
cout << "请输入系统密码:" << endl;
cin >> paword;
if (paword == "123456")
{
ads->m_Size = 0;
cout << "账号录已清空!" << endl;
}
else
{
cout << "密码错误" << endl;
return;
}
system("pause");
system("cls");
}
运行结果以及编译器报错