为银行项目计算读入/写出的问题
Issues figuring out readin/writeout for a bank program
我正在尝试制作一个银行程序,该程序可以从文本文件中读取用户名和密码,并将其与用户输入的内容进行比较。我已经搜索并尝试了几种不同的方法,但似乎无法使其发挥作用。很抱歉,如果这是重新发布的,我就是想不通。我对编码很陌生,所以请原谅我的错误。
我的文本文件名为:"Account.txt"第一行包含"username",第二行包含"userpassword"。
/*
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;
}
在newAccount函数中,您必须使用以下代码行存储用户名和密码。
ofstream outfile;
outfile.open("Account.txt");
outfile<<newusername<<endl;
outfile<<newpassword<<endl;
outfile.close();
在您的登录功能中,您必须在打开文件"Account.txt"后添加以下两行代码
inputFile>>customuser;
inputFile>>custompass;
这将解决目前获取单个用户的用户名和密码的问题。但是,您仍然需要找到一种方法来获取将使用您的应用程序的多个用户的用户名和密码。
一个更容易适应多个用户的方法是简单地将用户名和密码附加到文件中,然后依次搜索用户名和密码。
上述方法的替代方法是使用数据库来存储用户名和密码。
相关文章:
- Eulers 项目问题 no 345 听不懂几行代码
- 基本 Cuda C++项目集成问题
- C++ 从链表中删除项目时出现问题
- 双向链表问题 - 包含项目但不显示
- 在单元测试项目中包括 .c 文件,并从多个 cpp 文件访问它而不会出现链接问题
- 如何解决在 clr 项目的特定文件中写入数据的问题?
- 在Android Studio项目中使用tensorflow lite C++API的问题
- 相同的代码,不同的结果?从其他项目包含有什么问题?
- 我的自定义 C++ DLL 的 C# 项目中的 DLL 端口有什么问题?
- 在QtCreator中配置c ++项目时出现问题
- 编译示例 Cinder 项目时存在大量未解决的外部问题
- 将C库链接到Qt项目时出现问题
- VS 2017 C 静态LIB与 /MD项目依赖性问题编辑
- 测试项目在访问私有静态方法时遇到问题,尽管我从未直接调用过它
- 在 win32 控制台项目 VS2015 中静态链接 libcurl 的问题
- 鸟类调查中的项目在代码中遇到问题.如何像数据库系统一样存储数据
- 小型学校项目存在多个问题
- 为该项目报告C++奇怪的Klocwork问题
- 内存管理容器设计问题 - 项目需要继承
- 困难与问题.3项目欧拉(c++)