C++ 如何在2个void函数中将用户名和密码存储到文件中?
我要简化我的问题。。。如何实现If/else语句或任何语句,以获取用户从我的createaccount函数输入的数据,并根据登录函数进行验证?如果登录名与文件“accounts.txt”中的内容不匹配,它将无法工作,并返回到mainmenu功能C++ 如何在2个void函数中将用户名和密码存储到文件中?,c++,void,C++,Void,我要简化我的问题。。。如何实现If/else语句或任何语句,以获取用户从我的createaccount函数输入的数据,并根据登录函数进行验证?如果登录名与文件“accounts.txt”中的内容不匹配,它将无法工作,并返回到mainmenu功能 using namespace std; char mainMenu(); void createAccount(); void login(); char bankingMenu();
using namespace std;
char mainMenu();
void createAccount();
void login();
char bankingMenu(); // Prototypes Declared Here
void deposit();
void withdraw();
void displayBalance();
void ShowTransHist(); // Gets transaction history from the file path string and display
int main()
{
char choice;
while (choice != 'q' || choice != 'Q')
{
choice = mainMenu();
if (choice == 'q' || choice == 'Q') break;
switch (choice)
{
case 'l':
case 'L':
login();
break;
case 'c':
case 'C':
createAccount();
break;
case 'v':
case 'V':
cout << "Thank you for using our bank and Future Computer Programmer ATM Machine! \nFor your continued support, we are offering 3% cash back on all debit purchases." << endl;
}
}
return 0;
}
char mainMenu() // Function to display the main menu, not banking menu
{
char choice;
cout << "Please select an option from the menu below:" << endl;
cout << "l -> Login" << endl;
cout << "c -> Create New Account" << endl;
cout << "v -> View Promotions" << endl;
cout << "q -> Quit" << endl;
cout << "Enter your choice: " << endl;
cin >> choice;
return choice;
}
void createAccount() // Takes and stores users login information, username and password
{
string username;
string password;
cout << "Please create a username";
cin >> username;
cout << "Please create a password";
cin >> password;
ofstream createaccount;
createaccount.open("accounts.txt");
createaccount << username << " " << password;
createaccount.close();
cout << "Information saved" << endl;
}
void login() // Takes information stored in create account
{
string username;
string password;
cout << "Please enter your username";
cin >> username;
cout << "Please enter your password";
cin >> password;
ifstream savedaccount;
savedaccount.open("accounts.txt");
savedaccount >> username >> password;
savedaccount.close();
cout << "Login successful";
}
使用名称空间std;
char main menu();
void createAccount();
无效登录();
char bankingMenu();//此处声明的原型
无效存款();
无效撤回();
无效显示余额();
void showtranhist();//从文件路径字符串和显示中获取事务历史记录
int main()
{
字符选择;
while(choice!=“q”| choice!=“q”)
{
choice=main菜单();
if(choice=='q'| | choice=='q')中断;
开关(选择)
{
案例“l”:
案例“L”:
登录();
打破
案例“c”:
案例“C”:
createAccount();
打破
案例“v”:
案例“V”:
cout我的问题在于底部的登录函数没有解释您遇到的问题。我必须获取用户在CreateCount函数中输入的信息,并使用文件“account.txt”将其读入登录函数是的,我有。这并不能解释你有什么问题。搜索std::map。自学。这是一个更简单的容器,特别是对于这种类型的问题解决。std::map有键,每个键都有一个值,一个键不能重复(用户名),每个键都有一个值(密码)。易于插入、搜索、删除..等等。您不能做什么?保存到文件中?从文件中读取?了解文件的内容?建议:分离数据savedaccount>>用户名>>,“>>密码;