确定银行程序的读/写的问题
Posted
技术标签:
【中文标题】确定银行程序的读/写的问题【英文标题】:Issues figuring out readin/writeout for a bank program 【发布时间】:2016-02-24 10:48:03 【问题描述】:我正在尝试制作一个银行程序,该程序可以从文本文件中读取用户名和密码,并将其与用户输入的内容进行比较。我已经搜索并尝试了几种不同的方法,但我似乎无法让它发挥作用。对不起,如果这是重新发布,我只是想不通。 我对编码很陌生,所以请原谅我的错误。
我拥有的文本文件名为:“Account.txt” 它在第一行包含“用户名”,然后在第二行包含“用户密码”。
/*
Hunter Walker 11/10/2015
Banking Application with Input validation
Assignment 2
*/
//Headers
#include<iostream>
#include<string>
#include<cstdlib>
#include <fstream>
using namespace std;
//Variables
char input1, input2;
string username,userpassword, newusername, newuserpassword;
string customuser, custompass;
double amountin, amountout;
double total = 0;
//Function declarations
int bankingmenu();
int newaccount();
int login();
int main();
int mainmenu();
int deposit();
int withdraw();
int showbalance();
//Code to read from file
// The main menu for the banking application
int mainmenu()
cout << "Hi! Welcome to Future Computer Programmer ATM Machine!" << endl;
cout << "Please select an option from the menu below:" << endl;
cout << "l -> Login " << endl;
cout << "c -> Create New Account " << endl;
cout << "q -> Quit " << endl;
cin >> input1;
return 0;
// Function to allow the user to make a new account
int newaccount()
cout << "**********************************" << endl;
cout << "Welcome to the create an account menu!" << endl;
cout << "Please enter your desired username:" << endl;
cin >> newusername;
cout << "Please enter a password for your account:" << endl;
cin >> newuserpassword;
cout << "Thank you for creating an account with Future Computer Programmer ATM Machine!" << endl;
cout << "Your username is " << newusername << " and your password is " << newuserpassword << "." << endl;
cout << endl;
cout << "Reminder: Don't share your username or password with anyone." << endl;
cout << endl;
cout << "**********************************" << endl;
// Function to allow user to login to their account
int login()
cout << "Please enter username:" << endl;
cin >> username;
cout << "Please enter password:" << endl;
cin >> userpassword;
ifstream inputFile;
inputFile.open("Account.txt");
cout << customuser << " " << custompass << endl;
if (customuser == username && custompass == userpassword)
bankingmenu();
else
cout << "User name or password incorrect!" << endl;
cout << "Returning to main menu!" << endl;
return main();
inputFile.close();
return 0;
// The secondary menu for withdrawing/depositing/ or checking balance
int bankingmenu()
cout << "*******************************" << endl;
cout << "Please select an option from the menu below::" << endl;
cout << " d -> Deposit Money" << endl;
cout << " w -> Withdraw Money" << endl;
cout << " r -> Request Balance" << endl;
cout << " q -> Quit" << endl;
cin >> input2;
if (input2 == 'd')
deposit();
else if (input2 == 'w')
withdraw();
else if (input2 == 'r')
showbalance();
else if (input2 == 'q')
cout << "Returning to main menu! " << endl;
return main();
else
cout << "Please select a valid input and try again!" << endl;
return bankingmenu();
return 0;
// Function to allow to deposit to account
int deposit()
cout << "Please enter the amount of money you wish to deposit:" << endl;
cin >> amountin;
total = amountin + total;
cout << "The deposit was a success! Thanks for using Future Computer Programmer ATM Machine!" << endl;
return bankingmenu();
// Function to allow user to withdraw from account
int withdraw()
cout << "Please enter the amount you would like to withdraw:" << endl;
cin >> amountout;
if (total < amountout)
cout << "You can't withdraw more money than you have!" << endl;
cout << "Please select a different amount to withdraw." << endl;
return withdraw();
else
cout << "The amount has been withdrawn." << endl;
total = total - amountout;
return bankingmenu();
// Function to display the balance
int showbalance()
cout << "The balance in your account is $" << total << "." << endl;
return bankingmenu();
// The main function that calls all previous functions to run
int main()
mainmenu();
// Option to login
if (input1 == 'l')
login();
// Option to make a new account
else if (input1 == 'c')
newaccount();
// Option to exit program
else if (input1 == 'q')
cout << "Thanks for using the Future Computer Programmer ATM Machine! " << endl;
cout << "The program will now exit!" << endl;
return 0;
// Input validation
else
cout << "Please select a valid menu option and try again!" << endl;
return main();
return 0;
【问题讨论】:
【参考方案1】:在 newAccount 函数中,您必须使用以下代码行存储用户名和密码。
ofstream outfile;
outfile.open("Account.txt");
outfile<<newusername<<endl;
outfile<<newpassword<<endl;
outfile.close();
在您的登录功能中,您必须在打开文件“Account.txt”后添加以下两行代码
inputFile>>customuser;
inputFile>>custompass;
这将解决当前获取单个用户的用户名和密码的问题。但是,您仍然需要想办法为将使用您的应用程序的多个用户获取用户名和密码。
处理多个用户的更简单的方法是简单地将用户名和密码附加到文件中,然后按顺序搜索用户名和密码。
上述方法的替代方法是使用数据库来存储用户名和密码。
【讨论】:
以上是关于确定银行程序的读/写的问题的主要内容,如果未能解决你的问题,请参考以下文章