为银行项目计算读入/写出的问题

Issues figuring out readin/writeout for a bank program

本文关键字:问题 项目 计算 为银行      更新时间:2023-10-16

我正在尝试制作一个银行程序,该程序可以从文本文件中读取用户名和密码,并将其与用户输入的内容进行比较。我已经搜索并尝试了几种不同的方法,但似乎无法使其发挥作用。很抱歉,如果这是重新发布的,我就是想不通。我对编码很陌生,所以请原谅我的错误。

我的文本文件名为:"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;

这将解决目前获取单个用户的用户名和密码的问题。但是,您仍然需要找到一种方法来获取将使用您的应用程序的多个用户的用户名和密码。

一个更容易适应多个用户的方法是简单地将用户名和密码附加到文件中,然后依次搜索用户名和密码。

上述方法的替代方法是使用数据库来存储用户名和密码。